Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract key=value substring with regex

I would like to extract the value of id from a string. The string contains key-value pairs and the pairs are comma separated (no commas in actual values).

I would like to extract the value of id key, which is 123456789 in the sample input string below. The value of id is always a number.

reference="edd63cd8-cdf5-11ed-afa1-0242ac120002",args="one two three",id=123456789,someField="data",someOtherId=567890

How can I achieve this with regex?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can match id= followed by a sequence of non-comma characters.

Matcher m = Pattern.compile("id=([^,]+)").matcher(str);
if (m.find()) System.out.println(m.group(1));
else System.out.println("No match");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading