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

Regex for KeyValue pattern

I have to check if a string follows the following patterns:

Field1=value1
Field1=value1,Field2=value2
7645a=fds23,Field2=dsd$

The words ‘field1’, ‘value1’ don’t count, the important thing is that it has to be something=something and if there is more than 1, it should be a comma for each pair.

I reached the following 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

((\w+)[^=])=((\w+)[^=])

"Match any one or more word except if it has =, then there should be an = and then match any one or more word except if it has =".

The thing is, it does take the comma but I think is because of \w. I don’t think this is correct.

I’m using https://regexr.com/ to check for the correct regular expression.

>Solution :

If you need to match symbols like $, then don’t use \w. This satisfies all your conditions:

(?:([^,=\n]+)=([^,=\n]+))(?:,([^,=\n]+)=([^,=\n]+))*

Test Here

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