I have several strings in the rough form:
[some number with one or 2 digits] [some text] [a text which is ABC or BC] [some text]
String test = "12testABCtest";
Pattern p = Pattern.compile("([\\d]{1,2})([\\w]*)(ABC|BC)([\\w]*)");
But it gives me always “BC in the 3rd group instead of ABC. ( as it include the A in the previous group )
Do you have any idea how to do it?
thank you,
You can make the text match non-greedy:
Reference:
Source:
Patternjavadoc: Reluctant quantifiersBasically: reluctant quantifiers will match as little as possible, as opposed to the default greedy quantifiers that will match as much as possible. You get a reluctant quantifier by appending a
?to another quantifier.