正则表达式匹配任何字符,包括换行符
是否有正则表达式来匹配“包括换行符在内的所有字符”?
例如,在下面的正则表达式中,$2
没有输出,因为 (.+?)
在匹配时不包含新行。
$string = "START Curabitur mollis, dolor ut rutrum consequat, arcu nisl ultrices diam, adipiscing aliquam ipsum metus id velit. Aenean vestibulum gravida felis, quis bibendum nisl euismod ut.
Nunc at orci sed quam pharetra congue. Nulla a justo vitae diam eleifend dictum. Maecenas egestas ipsum elementum dui sollicitudin tempus. Donec bibendum cursus nisi, vitae convallis ante ornare a. Curabitur libero lorem, semper sit amet cursus at, cursus id purus. Cras varius metus eu diam vulputate vel elementum mauris tempor.
Morbi tristique interdum libero, eu pulvinar elit fringilla vel. Curabitur fringilla bibendum urna, ullamcorper placerat quam fermentum id. Nunc aliquam, nunc sit amet bibendum lacinia, magna massa auctor enim, nec dictum sapien eros in arcu.
Pellentesque viverra ullamcorper lectus, a facilisis ipsum tempus et. Nulla mi enim, interdum at imperdiet eget, bibendum nec END";
$string =~ /(START)(.+?)(END)/;
print $2;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您不想添加
/s
正则表达式修饰符(也许您仍然希望.
在正则表达式中的其他位置保留其原始含义),您也可以使用字符类。一种可能性:不是空格或本身就是空格的字符。换句话说,任何角色。
您还可以在正则表达式的一小部分中本地更改修饰符,如下所示:
If you don't want add the
/s
regex modifier (perhaps you still want.
to retain its original meaning elsewhere in the regex), you may also use a character class. One possibility:a character which is not a space or is a space. In other words, any character.
You can also change modifiers locally in a small part of the regex, like so:
将
s
修饰符 添加到正则表达式中以导致.
匹配换行符:Add the
s
modifier to your regex to cause.
to match newlines:这对我来说非常可读,并且匹配“任何字符或换行符”
它的行为与:
与应用 点
href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#advanced_searching_with_flags" rel="nofollow noreferrer">s 标志也可以这样应用:
另外,您还可以在末尾添加
?
以使正则表达式 eager (在第一个匹配处停止)(.|\n)*?
否则,只有
(.|\n)*
正则表达式是贪婪,最终可能会得到多个结束字符串:This is very readable to me and matches "any character or newline"
It behaves the same as:
and the same as applying the "s" flag (DOTALL) on the dot
The s flag can also be applied like this:
Plus you can also add a
?
to the end to make the regex eager (stop on the first match)(.|\n)*?
Otherwise with only
(.|\n)*
the regex is greedy and you can end up with multiple end_string's:是的,你只需要使
.
匹配换行符:Yeap, you just need to make
.
match newline :我喜欢使用一个空的否定集来匹配不在该组中的任何字符,因为它是空的,它将匹配包括换行符在内的任何字符。
如果您想要多个字符
或多个字符,
请在 JavaScript 中进行测试。
I like to use an empty negated set which matches any character not in the group, since it's empty it will match anything including newlines.
If you want more than zero characters
Or more than one
Tested in JavaScript.
请使用使用
/s
标志的其他答案,让.
匹配Perl v5.12 添加了
\N
作为字符班级尽管设置了/s
,但始终匹配除换行符之外的任何字符的快捷方式。这允许\n
拥有一个合作伙伴,例如\s
拥有\S
。这样,您就可以像类似的答案一样使用补码的两边:
[\n\N]
、[\s\S]
等等。但是,您还使用 javascript 对其进行了标记,它认为
\N
只是大写 N。Go with the other answers that use the
/s
flag to let the.
match every character inPerl v5.12 added the
\N
as a character class shortcut to always match any character except a newline despite the setting of/s
. This allows\n
to have a partner like\s
has\S
.With this, you can do like similar answers to use both sides of the complement:
[\n\N]
,[\s\S]
, and so on.However, you've also tagged this with javascript, which thinks
\N
is just capital N.您想使用“多行”。
You want to use "multiline".