2

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 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, it works perfectly ! Yours is better than mine that I had created :) (?<=Gamme=)(.*?)(?:, [A-Z][a-z]{1,}=)|(?<=Gamme=)[^\\}]+

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.