SCJP6 regex issue

此生再无相见时 提交于 2019-12-17 02:00:09

问题


I have issue with following example:

import java.util.regex.*;
class Regex2 {
    public static void main(String[] args) {
        Pattern p = Pattern.compile(args[0]);
        Matcher m = p.matcher(args[1]);
        boolean b = false;
        while(b = m.find()) {
            System.out.print(m.start() + m.group());
        }
    }
}

And the command line:

java Regex2 "\d*" ab34ef

Can someone explain me, why the result is: 01234456

regex pattern is d* - it means number one or more but there are more positions that in args[1],

thanks


回答1:


\d* matches 0 or more digits. So, it will even match empty string before every character and after the last character. First before index 0, then before index 1, and so on.

So, for string ab34ef, it matches following groups:

Index    Group
  0        ""  (Before a)
  1        ""  (Before b)
  2        34  (Matches more than 0 digits this time)
  4        ""  (Before `e` at index 4)
  5        ""  (Before f)
  6        ""  (At the end, after f)

If you use \\d+, then you will get just a single group at 34.



来源:https://stackoverflow.com/questions/18384930/scjp6-regex-issue

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