晕,matches怎么不好用啊

majy 2004-09-08 02:37:50
我为了匹配两个数字,第一个已经确定是1,我的程序是这样的,
String str = "11::123::13::";
if (str.matches("1\\d+")) \\这里没有返回true,为什么??
....
...全文
101 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
majy 2004-09-08
  • 打赏
  • 举报
回复
哦,我明白了,谢谢
majy 2004-09-08
  • 打赏
  • 举报
回复
St0ne82() 兄,那我的
matches("1\\d+")应该反回true的呀,为什么不是呢
St0ne82 2004-09-08
  • 打赏
  • 举报
回复
另外,matches是判断整个字符串是否匹配。Java doc中是这样描述的:

Attempts to match the entire input sequence against the pattern.

If the match succeeds then more information can be obtained via the start, end, and group methods.


Returns:
true if, and only if, the entire input sequence matches this matcher's pattern
St0ne82 2004-09-08
  • 打赏
  • 举报
回复
Pattern p = Pattern.compile("1\\d");//1+一位数字
Matcher m = p.matcher("11::123::13");
while(m.find()){
System.out.println(m.group());
}

这会印出:
11
12
13
majy 2004-09-08
  • 打赏
  • 举报
回复
为什么后面一定要加\\S*啊,我把matches("1\\d{1}\\S*")
改成matches("1\\d+\\S*")也是正确的,晕,前面不是已经匹配了吗?
sgdb 2004-09-08
  • 打赏
  • 举报
回复
matches("1\\d{1}\\S*")

62,614

社区成员

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

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