1

Want to capture multiple words from the strings where words are come in different order. I have tried to capture these words with below regex.

I have below strings and want to capture trackingID, code, msg and status. All these words are coming in different order.

ErrorString : trackingID=jskdf-77sdkj-oij4-kerj43-lklfds; key1=lksfjsdkjfkslkdjf;key2=skjfjdkfj43jrkjskd;key4=lksfjsdkjfkslkdjf;key4=skjfjdkfj43jrkjskd;{\"info\":{\"details\":[{\"code\":\"ABC23423\",\"messages\":\"msg:INVALID_ERROR\",\"timeStamp\":\"2019-08-30T15:03:43.668Z\"}],\"$httpStatus\":400},\"status\":400,\"test\":\"ABC2342fdsff3\"

ErrorString : key1=lksfjsdkjfkslkdjf;key2=skjfjdkfj43jrkjskd;key4=lksfjsdkjfkslkdjf;key4=skjfjdkfj43jrkjskd;{\"info\":{\"details\":[{\"code\":\"ABC23423\",\"timeStamp\":\"2019-08-30T15:03:43.668Z\"}],\"$httpStatus\":400},\"messages\":\"msg:INVALID_ERROR\",\"status\":400,\"test\":\"ABC2342fdsff3\"trackingID=jskdf-77sdkj-oij4-kerj43-lklfds;

ErrorString : trackingID=jskdf-77sdkj-oij4-kerj43-lklfds; key1=lksfjsdkjfkslkdjf;key2=skjfjdkfj43jrkjskd;key4=lksfjsdkjfkslkdjf;key4=skjfjdkfj43jrkjskd;{\"info\":{\"details\":[{\"code\":\"ABC23423\",\"timeStamp\":\"2019-08-30T15:03:43.668Z\"}],\"status\":400,\"$httpStatus\":400},\"messages\":\"msg:INVALID_ERROR\",\"test\":\"ABC2342fdsff3\"

Tried with this Regex :

(trackingID|\\"code\\"|\\"msg|\\"status\\")(=|:\\"|:)([^(;|\\"|,)]*)

But its not working.

Want to capture the values of trackingID, code, msg and status.

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
Nitin D
  • 49
  • 1
  • 5
  • 1
    This might help: https://stackoverflow.com/questions/31846438/java-regex-matching-multiple-occurrences – kshetline Sep 20 '19 at 16:58
  • 1
    Regex is wrong tool for this job. Just create a wrapper object that identifies the above json structure and use some library like jackson to feed data into it. It will be more readable and efficient. – Aniket Sahrawat Sep 20 '19 at 17:00
  • 1
    @AniketSahrawat That is not JSON. Sure it include something that is, but the `trackingID` value is not in the part that is JSON. – Andreas Sep 20 '19 at 17:11
  • Can you share the code that is not working? If I use your pattern and the examples I do get matches. https://regex101.com/r/lhhvi2/1 Is that not ok? Note that in Java you have to double escape the backslashes ( Example from generated code): `String regex = "(trackingID|\\\\\"code\\\\\"|\\\\\"msg|\\\\\"status\\\\\")(=|:\\\\\"|:)([^(;|\\\\\"|,)]*)";` – The fourth bird Sep 20 '19 at 18:23

1 Answers1

0

Maybe,

(?<=\btrackingID\b=)\s*[^;\r\n]*

might simply return the trackingID, and you could do similarly for others.

Demo 1

Or with some expression similar to,

(?<=\btrackingID\b=)(\s*[^;\r\n]*)|\\"code\\":\\"([^\\"\r\n]*)|(\\"status\\"):(\d*)|\\"msg:([^\\"\r\n]*)

Demo 2

Test

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class re{

    public static void main(String[] args){

        final String regex = "(?<=\\btrackingID\\b=)(\\s*[^;\\r\\n]*)|\\\\\"code\\\\\":\\\\\"([^\\\\\"\\r\\n]*)|(\\\\\"status\\\\\"):(\\d*)|\\\\\"msg:([^\\\\\"\\r\\n]*)";
        final String string = "ErrorString :  trackingID=jskdf-77sdkj-oij4-kerj43-lklfds; key1=lksfjsdkjfkslkdjf;key2=skjfjdkfj43jrkjskd;key4=lksfjsdkjfkslkdjf;key4=skjfjdkfj43jrkjskd;{\\\"info\\\":{\\\"details\\\":[{\\\"code\\\":\\\"ABC23423\\\",\\\"messages\\\":\\\"msg:INVALID_ERROR\\\",\\\"timeStamp\\\":\\\"2019-08-30T15:03:43.668Z\\\"}],\\\"$httpStatus\\\":400},\\\"status\\\":400,\\\"test\\\":\\\"ABC2342fdsff3\\\"\n\n\n\n"
             + "ErrorString :   key1=lksfjsdkjfkslkdjf;key2=skjfjdkfj43jrkjskd;key4=lksfjsdkjfkslkdjf;key4=skjfjdkfj43jrkjskd;{\\\"info\\\":{\\\"details\\\":[{\\\"code\\\":\\\"ABC23423\\\",\\\"timeStamp\\\":\\\"2019-08-30T15:03:43.668Z\\\"}],\\\"$httpStatus\\\":400},\\\"messages\\\":\\\"msg:INVALID_ERROR\\\",\\\"status\\\":400,\\\"test\\\":\\\"ABC2342fdsff3\\\"trackingID=jskdf-77sdkj-oij4-kerj43-lklfds;\n\n\n\n"
             + "ErrorString :  trackingID=jskdf-77sdkj-oij4-kerj43-lklfds; key1=lksfjsdkjfkslkdjf;key2=skjfjdkfj43jrkjskd;key4=lksfjsdkjfkslkdjf;key4=skjfjdkfj43jrkjskd;{\\\"info\\\":{\\\"details\\\":[{\\\"code\\\":\\\"ABC23423\\\",\\\"timeStamp\\\":\\\"2019-08-30T15:03:43.668Z\\\"}],\\\"status\\\":400,\\\"$httpStatus\\\":400},\\\"messages\\\":\\\"msg:INVALID_ERROR\\\",\\\"test\\\":\\\"ABC2342fdsff3\\\"";

        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        final Matcher matcher = pattern.matcher(string);

        while (matcher.find()) {
            System.out.println("Full match: " + matcher.group(0));
            for (int i = 1; i <= matcher.groupCount(); i++) {
                System.out.println("Group " + i + ": " + matcher.group(i));
            }
        }

    }
}

Output

Full match: jskdf-77sdkj-oij4-kerj43-lklfds
Group 1: jskdf-77sdkj-oij4-kerj43-lklfds
Group 2: null
Group 3: null
Group 4: null
Group 5: null
Full match: \"code\":\"ABC23423
Group 1: null
Group 2: ABC23423
Group 3: null
Group 4: null
Group 5: null
Full match: \"msg:INVALID_ERROR
Group 1: null
Group 2: null
Group 3: null
Group 4: null
Group 5: INVALID_ERROR
Full match: \"status\":400
Group 1: null
Group 2: null
Group 3: \"status\"
Group 4: 400
Group 5: null
Full match: \"code\":\"ABC23423
Group 1: null
Group 2: ABC23423
Group 3: null
Group 4: null
Group 5: null
Full match: \"msg:INVALID_ERROR
Group 1: null
Group 2: null
Group 3: null
Group 4: null
Group 5: INVALID_ERROR
Full match: \"status\":400
Group 1: null
Group 2: null
Group 3: \"status\"
Group 4: 400
Group 5: null
Full match: jskdf-77sdkj-oij4-kerj43-lklfds
Group 1: jskdf-77sdkj-oij4-kerj43-lklfds
Group 2: null
Group 3: null
Group 4: null
Group 5: null
Full match: jskdf-77sdkj-oij4-kerj43-lklfds
Group 1: jskdf-77sdkj-oij4-kerj43-lklfds
Group 2: null
Group 3: null
Group 4: null
Group 5: null
Full match: \"code\":\"ABC23423
Group 1: null
Group 2: ABC23423
Group 3: null
Group 4: null
Group 5: null
Full match: \"status\":400
Group 1: null
Group 2: null
Group 3: \"status\"
Group 4: 400
Group 5: null
Full match: \"msg:INVALID_ERROR
Group 1: null
Group 2: null
Group 3: null
Group 4: null
Group 5: INVALID_ERROR
Emma
  • 27,428
  • 11
  • 44
  • 69