Xavier DSouza
Xavier DSouza

Reputation: 2931

Java - Why is this pattern matching not working?

public static String FILL_IN_THE_BLANK_REGEX = "\\\\[blank_.+\\\\]";

public static int getBlankCountForFillInTheBlank(String questionText) {
    Matcher m = Pattern.compile(FILL_IN_THE_BLANK_REGEX).matcher(questionText);
    int count = 0;
    while (m.find()) ++count;

    return count;
}

public static void main(String[] args) {
   System.out.println(getBlankCountForFillInTheBlank("abc [blank_tag1] abc [blank_tag2]")); // prints 1
}

But if I do something like

public static String FILL_IN_THE_BLANK_REGEX = "\\\\[blank_tag.\\\\]";

It prints 2 which is correct.

'+' does not work here I don't know why.
(the blank tag can be anything like [blank_someusertag])

Upvotes: 1

Views: 79

Answers (2)

brandonscript
brandonscript

Reputation: 72855

.+ will match ANY character 1 or more times.

Use the non-greedy ? to ensure you only capture until the next defined expression.

Your working expression: \\[blank_.+?\\]

Upvotes: 3

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279910

See the javadoc for Pattern. I believe it's because + is a greedy quantifier and therefore matches everything it can. You can add a ? after the + to make it reluctant.

public static String FILL_IN_THE_BLANK_REGEX = "\\[blank_.+?\\]";

will print

2

Upvotes: 6

Related Questions