Regular expression - Greedy quantifier [duplicate]

余生颓废 提交于 2019-12-02 12:33:52

It is helpful to change

System.out.print(m.start() + m.group());

to

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

This way the output is much clearer:

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

You can see that it matched at 7 different positions: at position 2 it matched string "34" and at any other position it matched an empty string. Empty string matches at the end as well, which is why you see "6" at the end of your output.

Note that if you run your program like this:

java Regex2 "\d+" ab34ef

it will only output

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