简体   繁体   English

为什么这个正则表达式不返回任何东西?

[英]Why isn't this regular expression returning anything?

preg_match_all('/<p>.*:</p>/gm', $content, $matches);
var_dump($matches); //ouput is NULL

I want something like this: <p>The Ideal Candidate:</p> to match but not <p>Some more text</p> . 我想要这样的东西: <p>The Ideal Candidate:</p>匹配但不匹配<p>Some more text</p> The requirement being it must contain a <p> tag followed by some text and at the end it must contain a : followd by the end of the p tag ( </p> ). 要求是它必须包含一个<p>标记,后跟一些文本,最后必须包含一个: ,然后是p标记( </p> )的末尾。

Note: I tried escaping the ending p tag, but it is still not working. 注意:我尝试转义结尾的p标记,但仍然无法正常工作。 Updated code: 更新的代码:

preg_match_all('/<p>.*:<\/p>/gm', "<p>The Ideal Candidate:</p>", $matches);

Escape the / or use another delimiter 转义/或使用另一个定界符

 /<p>(.*?:)<\/p>/m

or 要么

 #<p>(.*?:)</p>#m

Tested: 测试:

preg_match_all('#<p>(.*?:)</p>#m', "<p>The Ideal Candidate:</p>", $m);
print_r($m)

output: 输出:

Array
(
    [0] => Array
        (
            [0] => <p>The Ideal Candidate:</p>
        )

    [1] => Array
        (
            [0] => The Ideal Candidate:
        )

)

g is not a valid modifier, see PHP: Pattern modifiers . g不是有效的修饰符,请参见PHP:模式修饰符 You should pay close attention to the warnings PHP issue. 您应该密切注意PHP问题警告。 When running 跑步时

preg_match_all("/<p>.*:<\/p>/gm", "<p>The Ideal Candidate:</p>", $matches);
print_r($matches);

I get 我懂了

Warning: preg_match_all() : Unknown modifier ' g ' in Command line code on line 1 警告: preg_match_all() :第1行的命令行代码中的未知修饰符' g '

Whereas the same line without the g modifier yields 而没有g修饰符的同一行会产生

Array
(
    [0] => Array
        (
            [0] => <p>The Ideal Candidate:</p>
        )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM