正则表达式判断单词是否存在

hmsuccess 2009-04-21 08:59:26
使用正则表达式判断几个给定的单词是否在一个单词序列中存在,条件是预先不知道单词出现的次数和顺序
如,String words="this is a perfect paper for the data management!"
String isExist_words="paper management";
判断words中是否出现paper management这两个单词,谢谢
...全文
659 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hbgzg3006 2009-04-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jcyan 的回复:]
if (words.contains("paper") && words.contains("managment"))
[/Quote]
这样判断pape也是存在的,manage也是存在的。
yangkaixin1226 2009-04-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jcyan 的回复:]
if (words.contains("paper") && words.contains("managment"))
[/Quote]
顶一楼的方法
PandaIT 2009-04-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jcyan 的回复:]
if (words.contains("paper") && words.contains("managment"))
[/Quote]

用包含!
kingssman 2009-04-21
  • 打赏
  • 举报
回复
这个正则表达式做不了,只能自己判断一个字符串里是不是包含另一个字符串
hbgzg3006 2009-04-21
  • 打赏
  • 举报
回复
//写一个笨的方法吧。
private static boolean isExist(){
boolean rtn=true;
String isExist_words="paper manage ";
String words = "this is a perfect paper paper for the data management!";
String regex="\\b\\w+\\b";
List<String> list1=new ArrayList<String>();
List<String> list2=new ArrayList<String>();
Matcher m1=Pattern.compile(regex).matcher(isExist_words);
while(m1.find()){
list1.add(m1.group());
}
Matcher m2=Pattern.compile(regex).matcher(words);
while(m2.find()){
list2.add(m2.group());
}
if(list1.size()<=0||list2.size()<=0)
rtn=false;
for(String i:list1){
if(!list2.contains(i))
rtn=false;
}
return rtn;
}
jcyan 2009-04-21
  • 打赏
  • 举报
回复
if (words.contains("paper") && words.contains("managment"))

62,614

社区成员

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

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