[英]Regex - Why doesn't this .* (dot-star) match line-breaks?
/(<pre>|<code>|\\[code])(.*)(</pre>|</code>|\\[/code])/gi
This works if I have something such as: 如果我有以下内容:
<code>foobar</code>
But if I were to have a line-break like this: 但如果我有这样的换行符:
<code>
Awesome
</code>
It will not match it, what am I doing wrong? 它不符合它,我做错了什么?
You do need the DOTALL
modifer /s
, because the .
你确实需要
DOTALL
modifer /s
,因为.
dot per default excludes linebreaks. 默认情况下,dot不包括换行符。 The
/g
modifier OTOH is not legal in PHP and PCRE. /g
修饰符OTOH在PHP和PCRE中不合法。
You should also use .*?
你也应该使用
.*?
to not match too wide. 不太匹配。
In PCRE, "." 在PCRE中,“。” does not match every character, it matches every thing that isn't a newline:
不匹配每个字符,它匹配所有不是换行符的东西:
Outside a character class, a dot in the pattern matches any one character in the subject, including a non-printing character, but not (by default) newline.
在字符类之外,模式中的点与主题中的任何一个字符匹配,包括非打印字符,但不是(默认情况下)换行符。
( http://www.php.net/manual/en/regexp.reference.dot.php ) ( http://www.php.net/manual/en/regexp.reference.dot.php )
Try something like [\\s\\S] instead. 尝试使用[\\ s \\ S]之类的东西。
Because .
因为
.
matches every character except newline by default, unless you feed in the s
switch. 除非您输入
s
开关,否则默认情况下匹配除换行符之外的每个字符。
See explanation of regex switches here . 请在此处查看正则表达式开关的说明。
In particular 尤其是
s (PCRE_DOTALL) If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines .
s(PCRE_DOTALL)如果设置了此修饰符,则模式中的点元字符将匹配所有字符,包括换行符 。 Without it, newlines are excluded.
没有它,排除了换行符。
So /(<pre>|<code>|\\[code])(.*)(</pre>|</code>|\\[/code])/is
. 所以/(<
/(<pre>|<code>|\\[code])(.*)(</pre>|</code>|\\[/code])/is
.*)(</ /(<pre>|<code>|\\[code])(.*)(</pre>|</code>|\\[/code])/is
。
(No g
, use preg_match_all
). (否
g
,使用preg_match_all
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.