I have this code:
String line = "There were bear#67 with dog#1323 and cat#5475 in the forest";
String pattern = ".* ([^ ]+)#(\\d{4}).*";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("Found value: " + m.group(0) );
System.out.println("Found value: " + m.group(1) );
System.out.println("Found value: " + m.group(2) );
} else {
System.out.println("NO MATCH");
}
It prints
Found value: There were bear#67 with dog#1323 and cat#5475 in the forest
Found value: cat
Found value: 5475
However I need to get all matches as ArrayList:
dog#1323, cat#5475
How would I do that?
String pattern = "[^ ]+#\\d{4}";and addmatcher.group(0)to a list inside the loopdog#1323, and Stringcat#5475