本文介绍了正则表达式匹配任何字符,包括新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有正则表达式匹配包括换行符在内的所有字符"?

Is there a regex to match "all characters including newlines"?

例如,在下面的正则表达式中,$2 没有输出,因为 (.+?) 在匹配时不包含新行.

For example, in the regex below, there is no output from $2 because (.+?) doesn't include new lines when matching.

$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;

推荐答案

添加 s 修饰符 到您的正则表达式以导致 . 匹配换行符:

Add the s modifier to your regex to cause . to match newlines:

$string =~ /(START)(.+?)(END)/s;

这篇关于正则表达式匹配任何字符,包括新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 20:29