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

Easy regex expression – taking the group up to the last comma

probably simple stuff but I don’t use regex that much.

Input: c1 = 7634, c2 = a,b, c3 = 8gd_, c4 = e,78

Desired Output:

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

c1 = 7634
c2 = a,b
c3 = 8gd_
c4 = e,78

Complexity (for me) – I don’t know how to define it so that for c2 it takes a,b with the comma. What I currently have:

(\w+)\s*=\s*((?:[^,].*?[^,])?)

trails the b.

Thanks upfront!

>Solution :

You can use

(\w+)\s*=\s*(.*?)(?=\s*,\s*\w+\s*=|$)

See the regex demo

Details:

  • (\w+) – Group 1: one or more word chars
  • \s*=\s* – a = sign enclosed with zero or more whitespaces
  • (.*?) – Group 2: any zero or more chars other than line break chars as few as possible
  • (?=\s*,\s*\w+\s*=|$) – immediately followed with a comma inside optional whitespaces + one or more word chars, zero or more whitespaces, and a = sign, or end of string.

Add \s* before $ if there can be trailing whitespaces.

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