Java regex - Determine which capture group was matched and count occurences

1.2k views Asked by At

Suppose that I want to build a very large regex with capture groups on run-time based on user's decisions.

Simple example:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {    
    static boolean findTag, findWordA, findOtherWord, findWordX;

    static final String TAG = "(<[^>]+>)";
    static final String WORD_A = "(wordA)";
    static final String OTHER_WORD = "(anotherword)";
    static final String WORD_X = "(wordX)";

    static int tagCount = 0;
    static int wordACount = 0;
    static int otherWordCount = 0;
    static int wordXCount = 0;

    public static void main(String[] args) {
        // Boolean options that will be supplied by the user
        // make them all true in this example
        findTag = true;
        findWordA = true;
        findOtherWord = true;
        findWordX = true;

        String input = "<b>this is an <i>input</i> string that contains wordX, wordX, anotherword and wordA</b>";

        StringBuilder regex = new StringBuilder();

        if (findTag)
            regex.append(TAG + "|");

        if (findWordA)
            regex.append(WORD_A + "|");

        if (findOtherWord)
            regex.append(OTHER_WORD + "|");

        if (findWordX)
            regex.append(WORD_X + "|");

        if (regex.length() > 0) {
            regex.setLength(regex.length() - 1);
            Pattern pattern = Pattern.compile(regex.toString());

            System.out.println("\nWHOLE REGEX: " + regex.toString());
            System.out.println("\nINPUT STRING: " + input);

            Matcher matcher = pattern.matcher(input);

            while (matcher.find()) {
                // only way I know of to find out which group was matched:
                if (matcher.group(1) != null) tagCount++;
                if (matcher.group(2) != null) wordACount++;
                if (matcher.group(3) != null) otherWordCount++;
                if (matcher.group(4) != null) wordXCount++;
            }

            System.out.println();
            System.out.println("Group1 matches: " + tagCount);
            System.out.println("Group2 matches: " + wordACount);
            System.out.println("Group3 matches: " + otherWordCount);
            System.out.println("Group4 matches: " + wordXCount);

        } else {
            System.out.println("No regex to build.");
        }
    }
}

The problem is that I can only count each group's matches only when I know beforehand which regex/groups the user wants to find.

Note that the full regex will contain a lot more capture groups and they will be more complex.

How can I determine which capture group was matched so that I can count each group's occurrences, without knowing beforehand which groups the user wants to find?

1

There are 1 answers

2
Scott Weaver On BEST ANSWER

construct the regex to used named groups:

(?<tag>wordA)|(?<wordx>wordX)|(?<anotherword>anotherword)