正则表达式匹配任何字符,包括换行符

发布于 2024-12-18 14:23:13 字数 1083 浏览 0 评论 0 原文

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

例如,在下面的正则表达式中,$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;

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

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

瘫痪情歌 2024-12-25 14:23:13

如果您不想添加 /s 正则表达式修饰符(也许您仍然希望 . 在正则表达式中的其他位置保留其原始含义),您也可以使用字符类。一种可能性:

[\S\s]

不是空格或本身就是空格的字符。换句话说,任何角色。

您还可以在正则表达式的一小部分中本地更改修饰符,如下所示:

(?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:

[\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:

(?s:.)
无可置疑 2024-12-25 14:23:13

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

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

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

$string =~ /(START)(.+?)(END)/s;
陈年往事 2024-12-25 14:23:13

这对我来说非常可读,并且匹配“任何字符或换行符

# any character or newline
(.|\n)*

它的行为与:

# \S (every non space) \s (every space, tab, newline, ...)
[\S\s]*

与应用

# Due to the s flag in the () capturing group, the dot . now matches any character including newlines
(?s:.)*

href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#advanced_searching_with_flags" rel="nofollow noreferrer">s 标志也可以这样应用:

/start_string(.*)end_string/s

另外,您还可以在末尾添加 ? 以使正则表达式 eager (在第一个匹配处停止) (.|\n)*?

// Eager (stop on first match)
start_string(.|\n)*?end_string

否则,只有 (.|\n)* 正则表达式是贪婪,最终可能会得到多个结束字符串:

start_string some text
and newlines end_string
some more text end_string

This is very readable to me and matches "any character or newline"

# any character or newline
(.|\n)*

It behaves the same as:

# \S (every non space) \s (every space, tab, newline, ...)
[\S\s]*

and the same as applying the "s" flag (DOTALL) on the dot

# Due to the s flag in the () capturing group, the dot . now matches any character including newlines
(?s:.)*

The s flag can also be applied like this:

/start_string(.*)end_string/s

Plus you can also add a ? to the end to make the regex eager (stop on the first match) (.|\n)*?

// Eager (stop on first match)
start_string(.|\n)*?end_string

Otherwise with only (.|\n)* the regex is greedy and you can end up with multiple end_string's:

start_string some text
and newlines end_string
some more text end_string
旧话新听 2024-12-25 14:23:13

是的,你只需要使 . 匹配换行符:

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

Yeap, you just need to make . match newline :

$string =~ /(START)(.+?)(END)/s;
因为看清所以看轻 2024-12-25 14:23:13

我喜欢使用一个空的否定集来匹配不在该组中的任何字符,因为它是空的,它将匹配包括换行符在内的任何字符。

[^]

如果您想要多个字符

[^]*

或多个字符,

[^]+

请在 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.

风筝在阴天搁浅。 2024-12-25 14:23:13

请使用使用 /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 in

Perl 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.

椒妓 2024-12-25 14:23:13

您想使用“多行”。

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

You want to use "multiline".

$string =~ /(START)(.+?)(END)/m;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文