Answer the question
In order to leave comments, you need to log in
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("НЕ СОВПАДАЕТ");
}
Какой то текст 19
9
Answer the question
In order to leave comments, you need to log in
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
.* - any character, including a number.
\\d+ - one or more characters.
That is why it does so: ( Some text 19 )( 9 ) and again some text?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question