来看看这个正则表达式

fhvsbgmy 2008-05-23 01:17:05
"xxfd.fff".matches(".*[^\\.].*")
结果总是不正确,我本意是想看字符串中是不是 “不包含” 字符“.”。

否定应该怎么写啊???



即:
String strPattern = "xxxxxxx";
"xxfdfff".matches(strPattern ) == true
"xxfd.fff".matches(strPattern ) == false
...全文
105 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
fhvsbgmy 2008-05-26
  • 打赏
  • 举报
回复
四楼第一个正解!!!!

差点没看见,哈哈!


fhvsbgmy 2008-05-26
  • 打赏
  • 举报
回复
六楼正解!!!

thanks!居然没想到这么写!!!

钻牛角尖了,嘿嘿
rascalboy520 2008-05-23
  • 打赏
  • 举报
回复
疯了,正则是用来找存在的,怎么拿它找不存在的????
还是我太笨了?????
简单的说,就是5楼的方法就可以满足了,LZ一定是在钻牛角尖!!!呵呵,
rascalboy520 2008-05-23
  • 打赏
  • 举报
回复
5楼正解!!!
jiaping108 2008-05-23
  • 打赏
  • 举报
回复

xxfd.fff".matches("[^\\.]*"); //正解

jiaping108 2008-05-23
  • 打赏
  • 举报
回复
可以直接在结果前加个非不就成了,何必非得钻牛角尖呢,呵呵
[Quote=引用楼主 fhvsbgmy 的帖子:]
"xxfd.fff".matches(".*[^\\.].*")
结果总是不正确,我本意是想看字符串中是不是 “不包含” 字符“.”。

否定应该怎么写啊???

[/Quote]

public static void _4to7(int x){
String strtest = "xxfdfff";
//正则,包含为false不包含输出true
boolean flag = strtest.matches(".*\\..*");
if(flag){
System.out.println("false");
}else{
System.out.println("true");
}

//非正则,包含为false不包含输出true
int index = strtest.indexOf(".");
if(index == -1){
System.out.println("true");
}else{
System.out.println("false");
}

}
chb865 2008-05-23
  • 打赏
  • 举报
回复
试试这个吧
看符不符合你的意思
String str1="xff.txt";
String str2="123";
String notContain="[^\\.]*";
String contain=".*\\..*";
System.out.println(str1+":"+str1.matches(notContain));
System.out.println(str2+":"+str2.matches(notContain));
System.out.println(str1+":"+str1.matches(contain));
System.out.println(str2+":"+str2.matches(contain));
ORACLE800 2008-05-23
  • 打赏
  • 举报
回复
刚刚我的做法貌似也不行
前后的点.就已经包括了你要排除的点了 , 所以也不行~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ORACLE800 2008-05-23
  • 打赏
  • 举报
回复
"xxfd.fff".matches(".*[^\\.].*")

改成"xxfd.fff".matches(".*?[^\\.].*?")

非贪婪模式
sunny_apple 2008-05-23
  • 打赏
  • 举报
回复
\b匹配一个字边界,即字与空格间的位置。例如,“er\b”匹配“never”中的“er”,但不匹配“verb”中的“er”。

\B非字边界匹配。“er\B”匹配“verb”中的“er”,但不匹配“never”中的“er”。

62,610

社区成员

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

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