匹配前面没有引号的模式

Match a pattern not preceded by a quotation mark

我有这个模式 (?<!')(\w*)\((\d+|\w+|.*,*)\) 用于匹配如下字符串:

根据 SO 上的一些答案,我添加了一个否定的回顾,这样如果输入字符串前面有 ',则该字符串根本不匹配。但是,它仍然部分匹配。

例如:

'c(4) returns (4) 尽管它不应该匹配任何东西,因为负向后看。

如果字符串前面有 ' 没有任何匹配项,我该如何做到这一点?

既然没人来,我就抛出这个让你开始吧。

这个正则表达式将匹配

aa(a , sd,,,f,)
aa( as , " ()asdf)) " ,, df, , )
asdf()

但不是

'ab(s)

这将解决基本问题 (?<!['\w])\w*
其中 (?<!['\w]) 不会让引擎跳过一个单词 char just
满足不引用.
然后optional words \w* 抓取所有的words.
如果前面有 'aaa( 引号,则它不会匹配。

这里的这个正则表达式修饰了我认为你正在努力完成的事情
在正则表达式的函数体部分。
一开始可能有点难以理解。

(?s)(?<!['\w])(\w*)\(((?:,*(?&variable)(?:,+(?&variable))*[,\s]*)?)\)(?(DEFINE)(?<variable>(?:\s*(?:"[^"\]*(?:\.[^"\]*)*"|'[^'\]*(?:\.[^'\]*)*')\s*|[^()"',]+)))

可读版本(来自:http://www.regexformat.com

 (?s)                          # Dot-all modifier

 (?<! ['\w] )                  # Not a quote, nor word behind
                               # <- This will force matching a complete function name
                               #    if it exists, thereby blocking a preceding quote '

 ( \w* )                       # (1), Function name (optional)
 \(
 (                             # (2 start), Function body
      (?:                           # Parameters (optional)
           ,*                            # Comma (optional)
           (?&variable)                  # Function call, get first variable (required)
           (?:                           # More variables (optional)
                ,+                            # Comma  (required)
                (?&variable)                  # Variable (required)
           )*
           [,\s]*                        # Whitespace or comma (optional)
      )?                            # End parameters (optional)
 )                             # (2 end)
 \)

 # Function definitions
 (?(DEFINE)
      (?<variable>                  # (3 start), Function for a single Variable
           (?:
                \s* 
                (?:                           # Double or single quoted string
                     "                            
                     [^"\]* 
                     (?: \ . [^"\]* )*
                     "
                  |  
                     '                      
                     [^'\]* 
                     (?: \ . [^'\]* )*
                     '
                )
                \s*     
             |                              # or,
                [^()"',]+                     # Not quote, paren, comma (can be whitespace)
           )
      )                             # (3 end)
 )