S
S
Sergey Khlopov2020-02-22 10:02:48
Java
Sergey Khlopov, 2020-02-22 10:02:48

Why does the regex group work the way it does?

Hello, I have the following code:

String str = "Какой то текст 199 и опять какой то текст ?";
String pattern = "(.*)(\\d+)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
   if(m.find( )) {
       System.out.println(m.group(1));
       System.out.println(m.group(2));
   } else {
        System.out.println("НЕ СОВПАДАЕТ");
    }

The conclusion is:
Какой то текст 19
9

And I don't understand why the first group of the regular expression "(.*)" ends with "Some text 19" why exactly here? I'm just now thinking that the first group should match the entire string. Please tell me why the regular season works this way, thanks in advance for the answer.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
agmt, 2020-02-22
@Shlop

Read about "lazy/greedy". "*" is greedy and tries to pick up as many characters as possible, but satisfying the entire expression is more important. "(.*?)(\\d+)" will behave differently

S
Sergey, 2020-02-22
@KingstonKMS

.* - any character, including a number.
\\d+ - one or more characters.
That is why it does so: ( Some text 19 )( 9 ) and again some text?

V
vreitech, 2020-02-22
@fzfx

> I'm just now thinking that the first group should match the entire string.
in this case, nothing remains for the second group.
remove the second group from the regular expression and you will have a match with the entire string.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question