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.
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_MPNmust be preceded by the start of string (or line, depending on your flags) or a comma. We do not want to match the comma orFEED_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.