Jonathan RIO
Jonathan RIO

Reputation: 21

Regex to find string between specific string and specific character

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 :

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 !

Upvotes: 2

Views: 40

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627536

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.

Upvotes: 1

Related Questions