I have following string “Class (102) (401)” and “Class (401)” i want to find regex to find substring which always return me value ‘Class’ out of “Class (102) (401)” or “Class (102)”
Following is my code
Pattern MY_PATTERN = Pattern.compile(".^(\\(\\d+\\))");
Matcher mat = MY_PATTERN.matcher("Class (102) (401)");
while (mat.find()){
System.out.println(mat.group());
}
I don’t see how that might be useful – but if you need it, the ‘class’ will be in the first matching group if you use this regex:
If you need to capture other words followed by the same pattern just use:
If it’s the numbers that you are after rather than the string ‘class’ use this one and the matches will be also in the capturing groups:
You’ll need to escape the backslashes, I left that out for readability.