2

I am struggling to understand a simple regex. I googled around. somehow it is not striking me.

Here is the method:

public static void testMethod(){
        String line = "This order was placed for QT3000! OK?";
        String pattern = "(.*)(\\d+)(.*)";

        // Create a Pattern object
        Pattern r = Pattern.compile(pattern);

        // Now create matcher object.
        Matcher m = r.matcher(line);
        if (m.find( )) {
           System.out.println("Found value: " + m.group(0) );
           System.out.println("Found value: " + m.group(1) );
           System.out.println("Found value: " + m.group(2) );
           System.out.println("Found value: " + m.group(3) );
        }
    }

Here is the output:

I was expecting group(2) to print 3000. but why it prints only 0.

2 Answers 2

6

Group 2 captured text only contains 0 because of the first greedy .*. It matched up to the last digit, and let \d+ have only the last digit. See demo of your regex.

To fix it, use lazy dot matching:

(.*?)(\d+)(.*)
   ^

See another demo

Sign up to request clarification or add additional context in comments.

1 Comment

See Watch Out for The Greediness! and The Greedy Trap for a comprehensive description of how backtracking works with greedy and lazy quantifiers. I just tried to shortly explain the root of the problem, and 1 possible way of fixing it. Surely, depending on what you need, you might use a negated character class (e.g. [^0-9]*), or tempered greedy token.
2

You need ([^0-9.]*)(\\d+)(.*).

The first group matching everything until last zero as you have + in second group. You need to escape numbers from the first group.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.