Regex to find string between specific string and specific character

Viewed 27

I am trying to find a string between specific string and specific character OR last occurence of character, using regex.

To be more clear, I need to find the value for the word "Gamme". That value can like "value" or like "value1,value2". My problem is mainly with the comma "," because the comma can be present in the value "value1,value2" but it is also the separator between each attribute in the string.

Example :

  • {Ambiance=undefined, Gamme=ADT COSY BLUE,CULINAIRE COSY BLUE}
  • {Ambiance=undefined, Style=undefined, Gamme=ADT COSY BLUE}
  • {Ambiance=undefined, Activité Produit=undefined, Gamme=undefined}
  • {Ambiance=undefined, Gamme=ADT COSY BLUE, Style=OCEAN}
  • {Ambiance=undefined, Gamme=ADT COSY BLUE,CULINAIRE COSY BLUE, Product=185427}

For now the best I could do is : (?<=Gamme=)(.*?)(?=[\=])|(?<=Gamme=)[^\}]+ But it does not work perfectly for my case.

Can you help me with the regex ?

Thanks !

1 Answers

You may use

(?<=Gamme=).*?(?=,\s*\w+=|[]}])

See the regex demo

Details

  • (?<=Gamme=) - a location right after Gamme=
  • .*? - any 0 or more chars other than line break chars, as few as possible
  • (?=,\s*\w+=|[]}]) - up to the first occurrence of
    • ,\s*\w+= - comma, 0 or more whitespaces, 1+ word chars and =
    • | - or
    • []}] - a ] or } char.
Related