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?
>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");