How Matcher.find() works [duplicate]

不羁的心 提交于 2019-11-28 10:55:18

Your regular expression can match zero characters. The final match is a zero width string occurring at the end of the string, after the character at index 5. The index of this zero width string is therefore 6.


As an aside, you might also find it easier to understand what is going on if you use separators to make the output more readable:

System.out.println(matcher.start()+ ": " + matcher.group());

Results:

0: 
1: 
2: 34
4: 
5: 
6: 

ideone

Your expression use * that means 0 or more digit, so can match no digit too.

Change your regular expression in this way

Pattern pattern = Pattern.compile("\\d+");

Using + means 1 or more.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!