Use the following idiom and link back to get value for your A, Band Caggregates:
String example = "your mobile number is <A> and username is <B> thanks <C>";
// ┌ left delimiter - no need to escape here
// | ┌ group 1: 1+ of any character, reluctantly quantified
// | | ┌ right delimiter
// | | |
Matcher m = Pattern.compile("<(.+?)>").matcher(example);
while (m.find()) {
System.out.println(m.group(1));
}
Output
A
B
C
Note
If you prefer a solution without indexed backlinks and “look-arounds”, you can achieve the same with the following code:
String example = "your mobile number is <A> and username is <B> thanks <C>";
// ┌ positive look-behind for left delimiter
// | ┌ 1+ of any character, reluctantly quantified
// | | ┌ positive look-ahead for right delimiter
// | | |
Matcher m = Pattern.compile("(?<=<).+?(?=>)").matcher(example);
while (m.find()) {
// no index for back-reference here, catching main group
System.out.println(m.group());
}
I personally find the latter less readable in this case.
source
share