匹配前面没有引号的模式

  • 本文关键字:模式 前面 regex pcre
  • 更新时间 :
  • 英文 :


我有这个模式(?<!')(w*)((d+|w+|.*,*)),旨在匹配以下字符串:

  • c(4)
  • hello(54, 41)

在 SO 的一些答案之后,我添加了一个负面的回溯,以便如果输入字符串前面有一个',字符串根本不应该匹配。但是,它仍然部分匹配。

例如:

'c(4)返回(4)即使由于负面的回溯而不应该匹配任何内容。

如果字符串前面有'NO 匹配,我该怎么做?

既然没有人来,我就把这个扔出去让你开始。

这个正则表达式将匹配类似的东西


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

但不是

'ab(s)

这将解决基本问题(?<!['w])w*
其中(?<!['w])不会让引擎跳过单词字符只是
为了满足not 引号
然后w*可选单词来抓取所有单词。
如果一个'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)
)

最新更新