如何用正则表达式从一个String中提取出英文单词?

FutureStonesoft 2004-11-19 11:53:08
String中可能含有中文等等任何字符,但是最终提取出来的是英文,
而且要考虑考he's或者low-income这类情况,也就是能吧
he's和low-inome都识别为一个单词,而不是拆分为he s low income四个单词。

比如有一个String如下:
"He's a boy.And she has a low-income.
------ 中国(china)是------He's a student."

提出出来的单词是:
He's
a
boy
And
she
has
a
low-income
china
student
...全文
946 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
stonecsdn 2004-11-19
  • 打赏
  • 举报
回复
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class PickWord{

public static void main(String args[]){
String regex = "([\\w]+(\\'|\\-)*[\\w]+)|([\\w]+)";
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
String article="He's a boy.And she has a low-income.";
Matcher matcher=pattern.matcher(article);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
treeroot 2004-11-19
  • 打赏
  • 举报
回复
http://blog.csdn.net/treeroot/archive/2004/11/11/176610.aspx

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧