简体   繁体   中英

Java regex extract key-value without quotes

I have a list of key-values as such:-

{
  "xxx" : "1234",
  "yyy" : "1234",
  "zzz" : "1234"
}

I have a regex pattern:-

String pattern = ".*zzz\\s*:\\s*(.*)[\n\r]";

that only extracts values in case of a match when key is 'zzz'.

However I don't get a match due to the quotes...how can I altern my pattern to get the match? Also, I don't want to get quotes in my value either.

You don't get the match due to the missing double quote after the zzz .

String pattern = ".*zzz\"\\s*:\\s*(.*)[\n\r]";
                        ^--- Here

Btw, You can use:

String pattern = ".*zzz\".*?\"(.*?)\"";

Then you have to grab the content from the capturing group through the Matcher match(1) .

Regex demo

IDE One demo

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