PHP 正则表达式任何字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4026213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:46:12  来源:igfitidea点击:

PHP Regex Any Character

phpregex

提问by Entity

The .character in a php regex accepts all characters, except a newline. What can I use to accept ALL characters, including newlines?

.php regex 中的字符接受除换行符之外的所有字符。我可以用什么来接受所有字符,包括换行符?

回答by Tomalak

This is commonly used to capture all characters:

这通常用于捕获所有字符:

[\s\S]

You could use any other combination of "Type-X + Non-Type-X" in the same way:

您可以以相同的方式使用“Type-X + Non-Type-X”的任何其他组合:

[\d\D]
[\w\W]

but [\s\S]is recognized by convention as a shorthand for "really anything".

[\s\S]被惯例认可为“真正的任何东西”的简写。

You can also use the .if you switch the regex into "dotall" (a.k.a. "single-line") mode via the "s"modifier. Sometimes that's not a viable solution (dynamic regex in a black box, for example, or if you don't want to modify the entireregex). In such cases the other alternatives do the same, no matter how the regex is configured.

.如果您通过"s"修饰符将正则表达式切换到“dotall”(又名“单行”)模式,您也可以使用。有时这不是一个可行的解决方案(例如,黑盒中的动态正则表达式,或者如果您不想修改整个正则表达式)。在这种情况下,无论正则表达式如何配置,其他替代方案都会执行相同的操作。

回答by Vincent Savard

It's the the .character that means "every character" (edit: OP edited). And you need to add the option s to your regexp, for example :

它的.字符意味着“每个字符”(编辑:OP 编辑​​)。并且您需要将选项 s 添加到您的正则表达式中,例如:

preg_match("`(.+)`s", "\n");

回答by gnomed

would

[.\n]+
[.\n]+

not work?

不行?

How about (.|\n)+? I tested it and it seems to work.

怎么样(.|\n)+?我测试了它,它似乎有效。

I am quite sure this is the literal interpretation of exactly what you were asking for.

我很确定这正是您所要求的字面解释。

回答by Franco

The PHP Manual page for Dotstates that:

Dot的 PHP 手册页面指出:

If the PCRE_DOTALL option is set, then dots match newlines as well.

如果设置了 PCRE_DOTALL 选项,则点也匹配换行符。

回答by steffen

An important thing is missing here. [\s\S]matches one character, whereas a newline can be a character sequence. (Windows uses two characters: \r\n.) Neither .(with DOT_ALLmodifier) nor [\s\S]will match the newline sequence. Best way to match any character or any newline is (.|\R), "everything except a newline or a newline". \Rmatches \n, \rand \r\n.

这里缺少一件重要的事情。[\s\S]匹配一个字符,而换行符可以是一个字符序列。(Windows 使用两个字符:\r\n.).(使用DOT_ALL修饰符)也[\s\S]不会匹配换行符序列。匹配任何字符或任何换行符的最佳方法是(.|\R)“除换行符或换行符之外的所有内容”。\R匹配\n,\r\r\n