0

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?

Abra
  • 19,142
  • 7
  • 29
  • 41
  • 1
    Your pattern works, but I think you have to use `while (m.find()) {` and get the value of group 1 and 2. See https://ideone.com/cBocRG – The fourth bird Jan 15 '21 at 07:56

2 Answers2

0

You are close. You need flag DOTALL and not flag MULTILINE. You also need to drop the start of line metacharacter, i.e. ^ and you need to use while instead of if to keep searching for the next occurrence of your pattern. Lastly, your regex only contains two groups.

Compare your code with the following.

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.DOTALL);

// Now create matcher object.
Matcher m = r.matcher(line);
boolean found = false;
while (m.find()) {
    found = true;
    System.out.println("Found value: " + m.group(1));
    System.out.println("Found value: " + m.group(2));
}
if (!found) {
    System.out.println("NO MATCH");
}

The output produced by the above code is:

Found value: ABC
Found value: 5149427501
Found value: JKL
Found value: 24938699
Abra
  • 19,142
  • 7
  • 29
  • 41
0

It seems that your string line is a json string。if so, I think the best practise is using jackson to resolve your problem。

You can see here my code here https://github.com/onemaomao/java-common-mistakes/blob/master/src/main/java/org/geekbang/time/questions/stackoverflow/reslove/q2/JacksonUsage.java

onemao
  • 16
  • 1
  • String line = "{\"ABC\":\"5149427501\",\"DEF\":4168170001,\"GHI\":\"RC81020329801823\",\"JKL\":\"24938699\",\"MNO\":\"941580078\"}"; ObjectMapper mapper = new ObjectMapper(); Map readValue = mapper.readValue(line, Map.class); System.out.println(readValue); System.out.println(readValue.get("ABC")); System.out.println(readValue.get("JKL")); – onemao Jan 15 '21 at 10:00