解释这个PHP正则表达式的含义(preg_match_all)

时间:2014-01-30 06:18:38

标签: php regex

PHP中的模式意味着什么:

'#^/abc/(?P<abc>[^/]++)$#si'; // please expand this pattern's meaning.

这个模式与preg_match_all的匹配项是什么?

1 个答案:

答案 0 :(得分:5)

这种模式'#^/abc/(?P<abc>[^/]++)$#si';如此分解:

/^\/abc\/(?P<abc>[^\/]++)$/si

^ assert position at start of the string
\/ matches the character / literally
abc matches the characters abc literally (case insensitive)
\/ matches the character / literally
(?P<abc>[^\/]++) Named capturing group abc
    [^\/]++ match a single character not present in the list below
        Quantifier: Between one and unlimited times, as many times as possible, without giving back [possessive]
        \/ matches the character / literally
$ assert position at end of the string
s modifier: single line. Dot matches newline characters
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

(来源:http://regex101.com/r/hS6qE3 - 请确保您在网站上/转义,因为它假设/是php分隔符。在您的模式示例中,#是而是分隔符,然后用引号和字符串终止符;包装。实际表达式只是^/abc/(?P<abc>[^/]++)$,带有单行和不区分大小写的修饰符。)

请注意使用两个++符号,changes the behavior from greedy to possessive

将匹配的字符串示例:

/abc/somethingelsenotafrontslash

您可以阅读有关preg_match_all here

的快速说明