Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php “(?…;)”和`(2)的正则表达式解释`_Php_Regex - Fatal编程技术网

Php “(?…;)”和`(2)的正则表达式解释`

Php “(?…;)”和`(2)的正则表达式解释`,php,regex,Php,Regex,我需要一些帮助来理解一点正则表达式。我看到的代码是这样的 preg_match("/^ (1[-\s.])? # optional '1-', '1.' or '1' ( \( )? # optional opening parenthesis \d{3} # the area code (?(2) \) ) # if there was opening parent

我需要一些帮助来理解一点正则表达式。我看到的代码是这样的

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)
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)
我完全理解,但不明白
(?(2)\)
到底是如何工作的。。。什么事?和(2)在tht中表示

问题更新

我读了你所有的答案。。当我像这样修改代码时

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)
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)表示条件#2,或者你可以说第二个捕获组,表示第二个
)中的条件。这意味着如果有
则必须有b

意味着如果第一个捕获组参与了到目前为止的匹配尝试,那么“then”部分必须匹配整个正则表达式才能匹配。如果第一个捕获组没有参与匹配,则“else”部分必须匹配才能匹配整个正则表达式

eg: (a)?(?(1)b|c) matches ab, the first c and the second c in babxcac
(2)表示条件#2,或者你可以说第二个捕获组,表示第二个
)中的条件。这意味着如果存在
,则必须存在b

意味着如果第一个捕获组参与了到目前为止的匹配尝试,那么“then”部分必须匹配整个正则表达式才能匹配。如果第一个捕获组没有参与匹配,则“else”部分必须匹配才能匹配整个正则表达式

eg: (a)?(?(1)b|c) matches ab, the first c and the second c in babxcac
(2)
表示第二个匹配片段,从这里开始:
(\()?
)。 所以整行代码是这样工作的:如果第二个片段是匹配的(表示有左括号),那么我们需要确保有右括号。

(2)
表示第二个匹配的片段,从这里开始:
(\()?
)。
所以整行是这样工作的:如果第二个片段匹配(意味着有左括号),然后我们需要确保有右括号。

猜测它说的是什么,寻找存储在第二个结果中的区号,如果是,它也应该被关闭。这意味着这可能是某种“如果结果2有效=>关闭,否则=>无”

正则表达式不是我最好的朋友,所以我希望我是对的。但是我也很难解释/创建它们,所以现在也许有人可以教我一件事;-)


顺便说一句,如果你在谷歌上搜索“PHP正则表达式备忘单”,可能会有一些你感兴趣的结果,至少对我来说是有趣的。

猜猜它说的是什么,寻找存储在第二个结果中的区号,如果是的话,它也应该关闭。这意味着这可能是一种工作方式'如果结果2有效=>close,否则=>nothing'

正则表达式不是我最好的朋友,所以我希望我是对的。但是我也很难解释/创建它们,所以现在也许有人可以教我一件事;-)


顺便说一句,如果你在谷歌上搜索“PHP正则表达式备忘单”,可能会有一些你感兴趣的结果,至少它们对我来说很有趣。

@pradeep最好应用
向前看
向后看
assertion@pradeep我用一个php链接更新了我的答案。这肯定会帮助你理解你是谁的概念talking@pradeep最好应用
向前看
向后看
assertion@pradeep我更新我的答案是一个php的链接。这肯定会帮助你理解你所说的概念