I've encountered the following token in a regular expression: [\s\S]*?
我在正则表达式中遇到了以下标记:[\ s \ S] *?
If I understand this correctly, the character class means "match a whitespace character or a non-whitespace character". Therefore, would this not do exactly the same thing as .*?
如果我理解正确,字符类意味着“匹配空白字符或非空白字符”。因此,这不会与。*完全相同吗?
One possible difference is that usually .
does not match newlines. However, this regular expression was written in Ruby and was passed the m
modifier meaning that the .
does, in fact, match newlines.
一个可能的区别是通常。与换行符不匹配。但是,这个正则表达式是用Ruby编写的,并且传递了m修饰符,意思是。事实上,确实匹配换行符。
Is there any other reason to use [\s\S]*?
instead of .*?
有没有其他理由使用[\ s \ S] *?代替 。*?
In case it helps, the regular expression I am looking at appears inside the sprockets library in the HEADER_PATTERN constant on line 97. The full expression is:
如果它有帮助,我正在查看的正则表达式出现在第97行的HEADER_PATTERN常量的sprockets库中。完整的表达式是:
/
\A \s* (
(\/\* ([\s\S]*?) \*\/) |
(\#\#\# ([\s\S]*?) \#\#\#) |
(\/\/ ([^\n]*) \n?)+ |
(\# ([^\n]*) \n?)+
)
/mx
3 个解决方案
#1
6
You interpreted the regex correctly.
您正确解释了正则表达式。
That seems like a relict from other languages which do not support the m-flag (or s-flag in other implementations).
这似乎是来自其他语言的遗留物,它们不支持m-flag(或其他实现中的s-flag)。
A reason to use that construct would be to not use the m-flag so you have the possibility to use . without matching newlines but are still able to match everything if need be.
使用该结构的一个原因是不使用m-flag,因此您可以使用。没有匹配换行符,但如果需要,仍然可以匹配所有内容。
#2
0
With the m flag, they would be the same except that .*
would be a lot clearer and easier to maintain.
使用m标志,它们将是相同的,除了。*将更清晰,更容易维护。
#3
0
The newline thing is the only difference. Maybe somebody thought it was easier to read without having to know the m context, or wanted it to be robust against a change to that context.
换行符是唯一的区别。也许有人认为在不必了解m上下文的情况下阅读更容易,或者希望它能够在更改上下文时保持健壮。
I have seen [^]*
used for a similar purpose.
我见过[^] *用于类似目的。
#1
6
You interpreted the regex correctly.
您正确解释了正则表达式。
That seems like a relict from other languages which do not support the m-flag (or s-flag in other implementations).
这似乎是来自其他语言的遗留物,它们不支持m-flag(或其他实现中的s-flag)。
A reason to use that construct would be to not use the m-flag so you have the possibility to use . without matching newlines but are still able to match everything if need be.
使用该结构的一个原因是不使用m-flag,因此您可以使用。没有匹配换行符,但如果需要,仍然可以匹配所有内容。
#2
0
With the m flag, they would be the same except that .*
would be a lot clearer and easier to maintain.
使用m标志,它们将是相同的,除了。*将更清晰,更容易维护。
#3
0
The newline thing is the only difference. Maybe somebody thought it was easier to read without having to know the m context, or wanted it to be robust against a change to that context.
换行符是唯一的区别。也许有人认为在不必了解m上下文的情况下阅读更容易,或者希望它能够在更改上下文时保持健壮。
I have seen [^]*
used for a similar purpose.
我见过[^] *用于类似目的。