Print regex matches in java

前端 未结 2 511
悲&欢浪女
悲&欢浪女 2020-12-03 15:57

So I have an IP address as a string. I have this regex (\\d{1-3})\\.(\\d{1-3})\\.(\\d{1-3})\\.(\\d{1-3}) How do I print the matching groups?

Thanks!

2条回答
  •  清歌不尽
    2020-12-03 16:34

    import java.util.regex.*;
    try {
        Pattern regex = Pattern.compile("(\\d\\{1-3\\})\\.(\\d\\{1-3\\})\\.(\\d\\{1-3\\})\\.(\\d\\{1-3\\})");
        Matcher regexMatcher = regex.matcher(subjectString);
        while (regexMatcher.find()) {
            for (int i = 1; i <= regexMatcher.groupCount(); i++) {
                // matched text: regexMatcher.group(i)
                // match start: regexMatcher.start(i)
                // match end: regexMatcher.end(i)
            }
        } 
    } catch (PatternSyntaxException ex) {
        // Syntax error in the regular expression
    }
    

提交回复
热议问题