Read Values after = from a comma separated string [closed]

I have a string first=abc123,second=status,z=3,a=45745 and want to get an output abc123,status,3,45745.

I could try to split it into multiple arrays and the get the values OR put this string into an ArrayList and split to get the result, but wanted to check if somebody already has a solution which does not involve a lot array creation or manipulation. Like a regex which could be helpful.

~TIA

Answer

.*?=([a-zA-Z0-9]*)

Regular expression visualization

Edit live on Debuggex

This will work. However, to make it better is there some constraints to the characters after or before the = sign?

For example,

[a-zA-Z]*?=([a-zA-Z0-9]*)

Regular expression visualization

This will only check if there is only letters at the start of the equals sign then letters/numbers after that and ignore the , all together. If it doesn’t meet those requirements it will fail.

If you know there is never going to be a number = to something i would suggest thing answer.

NOTE: Everything you will need will be captured in what regex’s calls a capture group which is denoted by ().

Leave a Reply

Your email address will not be published. Required fields are marked *