0

I have the below java string in the below format.

externalCustomerID: { \"custToken\": \"xyz\" }

I want to extract xyz value from above string. can anyone suggest me any regex expression for that in java?

user16320675
  • 135
  • 1
  • 3
  • 9
Yogesh
  • 89
  • 2
  • 6
  • is this the string "custToken": "xyz" or it's externalCustomerID: { "custToken": "xyz" } –  Aug 18 '21 at 06:31
  • externalCustomerID: { "custToken": "xyz" } this is my input string – Yogesh Aug 18 '21 at 06:33
  • you can use `JSONParser` to read the value. Go through below link to understand more about the JSONParser. https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – Nagaraju Chitimilla Aug 18 '21 at 06:35

1 Answers1

1

check this one

        Pattern pattern = Pattern.compile("(\\w+: \\{ \"\\w+\": \")(\\w+)");

    Matcher matcher = pattern.matcher("externalCustomerID: { \"custToken\": \"xyz\" }");

    if (matcher.find()) {
        System.out.println(matcher.group(2));
    }
  • you welcome :) you can use this site to test and verify your regex https://regex101.com/ make sure to choose java flavor from the left side –  Aug 18 '21 at 06:44
  • sorry @user16320675 it did not get what you mean –  Aug 18 '21 at 19:55