java中的正则表达式不能捕获最后一组

时间:2017-12-04 10:33:02

标签: java android regex regex-group

我有正则表达式,分为两组USD88和88美元

  ([a-zA-Z]+)(\d+.?\d*)

但在Java中,我无法获得最后一组。 Java代码摘录:

        Pattern pattern = 
        Pattern.compile("([a-zA-Z]+)(\\d+.?\\d*)");

        System.out.println("Enter input string to search: ");
        Matcher matcher = 
        pattern.matcher(bufferedReader.readLine());

        while (matcher.find()) {
            for(int i=0;i<matcher.groupCount();i++) {
                 System.out.format("capture group " +i+
                         " \"%s\" starting at " +
                         "index %d and ending at index %d.%n",
                         matcher.group(i),
                         matcher.start(i),
                         matcher.end(i));
            }
        }

示例:

 Enter input string to search: 
 USD100
 capture group 0 "USD100" starting at index 0 and ending at index 6.
 capture group 1 "USD" starting at index 0 and ending at index 3.
 Enter input string to search: 
 USD55.7
 capture group 0 "USD55.7" starting at index 0 and ending at index 7.
 capture group 1 "USD" starting at index 0 and ending at index 3.

0 个答案:

没有答案