简体   繁体   中英

How extract only numbers between double quotes, simple quotes or without them using regex

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.

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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM