Match string between () and not inside quotes

Viewed 37

My current attempt (?<=[,(][^"]).*(?=[,)][^"])

("WideCharToMultiByte", "uint", 0, "uint", 0x400, "uint", Address, "int", Length, "str", String, "int", char_count, "uint", 0, "uint", 0)  

https://regex101.com/r/Vd3w7C/3/

How do I match everything inside the ( ) less things inside quotes?

Match only: 0 address length string char_count 0 0

1 Answers

Using a PCRE/Onigmo compatible regex engine, you can use

(?:\G(?!^),\s*|\()(?:,?\s*"[^"]*")*,\s*\K[^\s,)]+

See the regex demo.

Details:

  • (?:\G(?!^),\s*|\() - either the end of the previous successful match and then a , and zero or more whitespaces, or a ( char
  • (?:,?\s*"[^"]*")* - zero or more occurrences of
    • ,? - an optional comma
    • \s* - zero or more whitespaces
    • "[^"]*" - a ", zero or more chars other than " and then a " char
  • , - a comma
  • \s* - zero or more whitespaces
  • \K - match reset operator discarding the text matched so far in the overall match memory buffer
  • [^\s,)]+ - one or more chars other than whitespace, , and ).
Related