Home > database >  Regex - Get all commas from text expression besides ones in functions
Regex - Get all commas from text expression besides ones in functions

Time:10-19

I have a problem with regex expression. I wan't to find all commas "," in text expression but not ones in functions. The text expression is something like 2,5 x min(2,3,4)

So:

2,3 #finds comma

(2,3) #finds comma

min(2,3) #ignores

The regex that I've tried to write only excludes brackets from my expression and looks like /,(?![^(]*\))/g but exclude only the function brackets like min(2,4,5)

Thanks!

CodePudding user response:

You can use

(?<!\w\([^()]*),

Details:

  • (?<!\w\([^()]*) - a negative lookbehind that matches a location that is not immediately preceded with a word char, ( and then zero or more chars other than ( and ).
  • , - a comma.

See the regex demo.

  • Related