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

How to parse a given key-value parameters string?

I have this string:

Parameter1="Something related to this" Parameter2="Another value" Parameter3=’Single quotes are permitted’ Parameter4="Even HTML entities are permitted"

I want to get this list:

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

  1. Parameter1=Something related to this
  2. Parameter2=Another value
  3. Parameter3=Single quotes are permitted
  4. Parameter4=Even HTML entities are permitted

I tried this regex. But it’s not working:

(\w+)=(('|"|")).*(('|"|"))

How can I parse this string and extract key-value pairs?

>Solution :

You can use

(\w+)=(['"]|")(.*?)\2

See the regex demo. Details:

  • (\w+) – Group 1: one or more word chars
  • = – an equals sign
  • (['"]|") – Group 2: ', " or "
  • (.*?) – Group 3: any zero or more chars other than an LF char as few as possible
  • \2 – Group 2 value.
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