PHP RegExp:如何在表达式中使用换行符? [英] php RegExp: how to use newline in expression?
问题描述
例如,它起作用:
{<div\s+class=\"article\"><h2(.*)</div>}s
如果我这样做,我什么也没得到:
If I do this way, I get nothing:
{<div\s+class=\"article\">
<h2(.*)
</div>}s
I suspect that I should use some modifier, but I know which one from here: http://php.net/manual/en/reference.pcre.pattern.modifiers.php
推荐答案
这将是/x
修饰符:
x (PCRE_EXTENDED)
x (PCRE_EXTENDED)
如果设置了此修饰符,则模式中的空白数据字符将被完全忽略(除非进行转义或在字符类内部),并且在字符类外部的未转义的#与下一个换行符(包括首字符)之间的字符也将被忽略.这等效于Perl的/x修饰符,并可以在复杂的模式中包含注释.但是请注意,这仅适用于数据字符.空格字符可能永远不会出现在模式中的特殊字符序列中,例如,在序列中(?(会引入条件子模式.
If this modifier is set, whitespace data characters in the pattern are totally ignored except when escaped or inside a character class, and characters between an unescaped # outside a character class and the next newline character, inclusive, are also ignored. This is equivalent to Perl's /x modifier, and makes it possible to include comments inside complicated patterns. Note, however, that this applies only to data characters. Whitespace characters may never appear within special character sequences in a pattern, for example within the sequence (?( which introduces a conditional subpattern.
它还允许注释模式,这非常有用:
It also allows commenting the pattern, which is extremely useful:
{<div\s+class=\"article\"> # many spaces between the div and the attribute
<h2(.*) # don't really care about closing the tag
</div>}sx
这篇关于PHP RegExp:如何在表达式中使用换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!