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 to match a specific text between a string and a comma (in a string with different values separated by commas)

Considering a string composed by several comma separated tags, I have to match/extract the text after a specific prefix. For example I have this string:

CAT:SPORT_BAMBINO,CAT:SPORT_SNEAKERS,COLOR:BIANCO,EV1,FEED-MPN-GW6511,GENDER:RAGAZZO,GENDER_GROUP:RAGAZZO,NC_SPORT,Omnibus: Not on sale,PRZ:BEST SELLER:B63DBA,R:4,SCARPE-SPORT,SEASON:SS23,SUBTYPE:S3540,TG:ADI_S_KIDS

I highlighted the text/tag I need to match in bold; in this case I have to match/extract: GW6511

The prefix I have always and is FEED-MPN- and I have to match/extract the text after this prefix and till the next comma.

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

I try different solutions to write the RegEx but I was not able to find a right expression that could fit any string/tags there can be (I have thousands of different items).

>Solution :

Assuming FEED-MPN-<X> can also be the first or last item in the "list", I propose the following RegEx:

(?:(?:^|,)FEED-MPN-)(.+?)(?:,|$)

Explanation:

  • FEED_MPN must be preceded by the start of string (or line, depending on your flags) or a comma. We do not want to match the comma or FEED_MPN, so we use non-capturing groups (?:...).
  • Now comes the suffix (.+?) which we want to lazily match until…
  • … we encounter the end of string/line or a comma, which we don’t want to match.
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