Is there a regex to match "all characters including newlines"?
是否有一个regex来匹配“包括换行符在内的所有字符”?
For example, in the regex below, there is no output from $2
because (.+?)
doesn't include new lines when matching.
例如,在下面的regex中,$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;
4 个解决方案
#1
129
Add the s
modifier to your regex to cause .
to match newlines:
将s修饰符添加到正则表达式以引起。匹配换行:
$string =~ /(START)(.+?)(END)/s;
#2
215
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:
如果您不想添加/s正则表达式修饰符(也许您仍然需要)。为了在regex中保留其原始含义),您还可以使用字符类。一种可能性:
[\S\s]
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:
您还可以在regex的一小部分本地更改修饰符,例如:
(?s:.)
#3
5
Yeap, you just need to make .
match newline :
是的,你只需要。匹配换行符:
$string =~ /(START)(.+?)(END)/s;
#4
-2
You want to use "multiline".
您需要使用“multiline”。
$string =~ /(START)(.+?)(END)/m;
#1
129
Add the s
modifier to your regex to cause .
to match newlines:
将s修饰符添加到正则表达式以引起。匹配换行:
$string =~ /(START)(.+?)(END)/s;
#2
215
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:
如果您不想添加/s正则表达式修饰符(也许您仍然需要)。为了在regex中保留其原始含义),您还可以使用字符类。一种可能性:
[\S\s]
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:
您还可以在regex的一小部分本地更改修饰符,例如:
(?s:.)
#3
5
Yeap, you just need to make .
match newline :
是的,你只需要。匹配换行符:
$string =~ /(START)(.+?)(END)/s;
#4
-2
You want to use "multiline".
您需要使用“multiline”。
$string =~ /(START)(.+?)(END)/m;