使用非

欢马劈雪     最近更新时间:2020-01-02 10:19:05

437
在正则表达式中使用非捕获组
/**
来 自 时代Java - nowjava.com
**/

import java.util.regex.Matcher;

import java.util.regex.Pattern;


public class Main {

  public static void main(String[] args) throws Exception {

    String inputStr = "abbabcd";

    String patternStr = "(a(?:b*))+(c*)";

    // (?:b*) is a non-capturing group


    // Compile and use regular expression

    Pattern pattern = Pattern.compile(patternStr);

    Matcher matcher = pattern.matcher(inputStr);

    boolean matchFound = matcher.find();


    if (matchFound) {

      // Get all groups for this match

      for (int i = 0; i <= matcher.groupCount(); i++) {

        String groupStr = matcher.group(i);

      }
      /**
展开阅读全文