I have this sample string
ABC : 5149427501\nDEF : 4168170001\nGHI : RC81020329801823\nJKL : 24938699\nMNO : 941580078
I want to extract the value corresponding to strings ABC
and JKL
using regex.
So the pattern should return 5149427501
as match for ABC
and 24938699
as match for JKL
.
I have been trying to formulate the regex and code for that but it is not working.
String line = "ABC : 5149427501\nDEF : 4168170001\nGHI : RC81020329801823\nJKL : 24938699\nMNO : 941580078";
String pattern = "^\\s*(ABC|JKL)\\s*:\\s*(\\d+)\\s*";
// Create a Pattern object
Pattern r = Pattern.compile(pattern, Pattern.MULTILINE);
// Now create matcher object.
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) );
System.out.println("Found value: " + m.group(3) );
System.out.println("Found value: " + m.group(4) );
} else {
System.out.println("NO MATCH");
}
But it is not working.
I am beginner in regex. Can you please suggest how to find the pattern in the String?