[英]regular expression explanations for `(?…)` and `(2)`
我需要一些帮助来理解一些正则表达式。 我看到了类似的代码
preg_match("/^
(1[-\s.])? # optional '1-', '1.' or '1'
( \( )? # optional opening parenthesis
\d{3} # the area code
(?(2) \) ) # if there was opening parenthesis, close it
[-\s.]? # followed by '-' or '.' or space
\d{3} # first 3 digits
[-\s.]? # followed by '-' or '.' or space
\d{4} # last 4 digits
$/x",$number)
我了解所有内容,但不了解(?(2) \\) )
实际工作原理。 和(2)中的代表。
问题更新...
我读了所有答案..当我更改代码时
preg_match("/^
(1[-\s.])? # optional '1-', '1.' or '1'
\d{3} # the area code
( \( )? # optional opening parenthesis
(?(3) \) ) # if there was opening parenthesis, close it
[-\s.]? # followed by '-' or '.' or space
\d{3} # first 3 digits
[-\s.]? # followed by '-' or '.' or space
\d{4} # last 4 digits
$/x",$number)
我收到类似的错误
Compilation failed: reference to non-existent subpattern
代码有什么错误吗?
(2)
表示第二个匹配的片段,从这里开始: ( \\( )?
因此整行都是这样的:如果第二个片段被匹配(意味着有开括号),那么我们需要确保有闭括号。
猜猜它说了什么,寻找存储在第二个结果中的区号,如果是,还应该将其关闭。 这意味着这可能类似于“如果result-2有效=>关闭,否则=>什么都没有”。
正则表达式不是我最好的朋友,所以我希望我是对的..但是我也很难解释/创建它们,所以也许现在任何人都可以在这里教我一件事;-)
顺便说一句,如果您在Google上搜索“ PHP正则表达式速查表”,则会发现一些安静的结果可能是您感兴趣的,至少对于我来说这些结果很有趣。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.