Search code examples
regexstringdelimiterquantifiers

Regex quantifiers - as many between two characters


I'm trying to find all the text between two characters but sometimes the string contains the delimiter character.

For example, If I use

(.*?)=(.*?),*

On the following string:

color=blue,weight=100kg,

It will result in:

match #1: color=blue
match #2: weight=100kg

However, if I have the following string:

color=blue,red,weight=100kg,

It will result in:

match #1: color=blue
match #2: red,weight=100kg

How can I make the regex return the following? (cutting the string at the last occurrence of the comma character)

match #1: color=blue,red
match #2: weight=100kg

Please note that the amount of colors separated by a comma could be more than 3, or none at all.

Thanks in advance,


Solution

  • You might use this regex:

    [^,]+=[^=]+(?=,) where

    [^,]+ - parameter name

    [^=]+(?=,) - this will capture parameter value, that allowed to contain everything, but = symbol and should ends with comma.

    regex101 example