正则表达式--Group

前端之家收集整理的这篇文章主要介绍了正则表达式--Group前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainClass {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("(\\d{3,5})([a-z]{2})");
        String string = "123aa-456bb-789cc-2fv";
        Matcher matcher = pattern.matcher(string);
        System.out.println(matcher.groupCount());
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
        matcher.reset();
        while (matcher.find()) {
            System.out.println(matcher.group(1));
        }
        matcher.reset();
        while (matcher.find()) {
            System.out.println(matcher.group(2));
        }
    }
}

output:

2
123aa
456bb
789cc
123
456
789
aa
bb
cc

猜你在找的正则表达式相关文章