[\S\s]* 在 PHP 的正则表达式中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4544636/
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
What does [\S\s]* mean in regex in PHP?
提问by yoyo
What is meant by [\s\S]*in regex in PHP? Does [\s\S]*actually match every string the same as .*?
[\s\S]*PHP 中的正则表达式是什么意思?[\s\S]*实际上是否匹配每个字符串都与.*?
回答by Kobi
By default .doesn't match new lines - [\s\S]is a hack around that problem.
This is common in JavaScript, but in PHP you can use the /sflag to to make the dot match all characters.
默认情况下.不匹配新行 -[\s\S]是解决该问题的一种方法。
这在 JavaScript 中很常见,但在 PHP 中,您可以使用/s标志使点匹配所有字符。
回答by codaddict
The .meta character matches any character except a newline. So the pattern .*which is used to match anything will not work if you have to match newlines as-well.
的.元字符匹配除换行符之外的任何字符。因此,.*如果您还必须匹配换行符,则用于匹配任何内容的模式将不起作用。
preg_match('/^.*$/',"hello\nworld"); // returns 0
[\s\S]which is a character class of white-space characters and non-whitespace characters matches anycharacter including a newline so do [\d\D], [\w\W]. So your pattern [\s\S]*now matches anything.
[\s\S]这是空白字符和非空白字符的字符类匹配包括换行符在内的任何字符[\d\D], , [\w\W]. 所以你的模式[\s\S]*现在匹配任何东西。
preg_match('/^[\s\S]$/s',"hello\nworld"); // returns 1
An alternative to make .match anything (including a newline) is to use a smodifier.
.匹配任何内容(包括换行符)的另一种方法是使用s修饰符。
preg_match('/^.*$/s',"hello\nworld"); // returns 1
Alternative way of using the smodifier is in-lining it as:
使用s修饰符的另一种方法是将其内联为:
preg_match('/^(?s).*(?-s)$/',"hello\nworld"); // returns 1
(?s)turns on the smode and (?-s)turns if off. Once turned off any following .will not match a newline.
(?s)打开s模式,(?-s)如果关闭则关闭。一旦关闭,任何后续.将不匹配换行符。
回答by Arley
[\s\S]A character set that matches any character including line breaks.
[\s\S]匹配任何字符(包括换行符)的字符集。
.Matches any character except line breaks.
.匹配除换行符以外的任何字符。

