The problem is this: you accidentally replaced using the regex pattern and expression to validate
String exp = "ABCD_123_abc"; String regExp = "([a-zA-Z]+)_([0-9]+)_([az]+)";
Should use
Pattern pattern = Pattern.compile(regExp); Matcher matcher = pattern.matcher(exp);
The Pattern.compile (String regex) function accepts a regular expression.
EDIT
I apologize, my first decision really was something that should never, never, never have been: the names of the variables were contradictory with the meaning of their values ... This means pain and tears, and the angry colleagues getting into a scream. And there is no real defense of this crime ...
EDIT2 You can get the individual mapped groups using the Matcher.group (int) function:
String matchedStringpart matcher.group(2);
Note. I used 2 as an argument:
0 means match input sequence1 means the first group ( ABC in this case)- ... etc.
If you only need part 123 , I would rewrite the regex for clarity:
regExp = "[a-zA-Z]+_([0-9]+)_[az]+";
However, in this case, group() must be called using 1 , since now the first (and only) consistent group is the first:
String matchedStringpart matcher.group(1);
ppeterka
source share