Home > Back-end >  How extract only numbers between double quotes, simple quotes or without them using regex
How extract only numbers between double quotes, simple quotes or without them using regex

Time:08-02

I have a Java code that processes events in real time, from which it extracts information using regex, recently it fails, after hours of debugging, I find that this value can arrive in the 3 ways that I leave here:

{"eventId":"25","started":"2022-07-29T18:37:38.717Z","finished":"2022-07-29T18:37:38.717Z","duration":0,"sessionId":"xxxx","personId":"1111111","status":"ok"}
{"eventId":'25',"started":"2022-07-29T18:37:38.717Z","finished":"2022-07-29T18:37:38.717Z","duration":0,"sessionId":"xxxx","personId":"1111111","status":"ok"}
{"eventId":25,"started":"2022-07-29T18:37:38.717Z","finished":"2022-07-29T18:37:38.717Z","duration":0,"sessionId":"xxxx","personId":"1111111","status":"ok"}

I have tried without success to modify the current regex that extracts the information, to extracts the eventId number 25 in any of the three cases:

(?<="eventId":)"(\w )"(?=,)

Is there any way to achieve this? ... thanks in advance!

PS: That is a Dataflow java code and just need extract the number without double or single quotes.

CodePudding user response:

If JSON parser is not available then here is a regex solution.

Java allows limited dynamism in lookbehind. You may use this regex:

(?<=\"eventId\":[\"']?)\w 

RegEx Demo

Note addition of an optional " or ' inside the lookbehind condition.

Code:

Pattern pattern = Pattern.compile("(?<=\"eventId\":[\"']?)\\w ");
Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
   System.out.println("Match: "   matcher.group(0));
}
  • Related