简体   繁体   English

正则表达式 - 。* $与。*相同。*

[英]Regular Expression - is .*$ the same as .*

I am cleaning up another person's regular expressions and they currently end all of theirs with 我正在清理另一个人的正则表达,他们现在结束了他们的所有

.*$

So wouldn't the following be exactly the same? 所以下面的内容不一样吗?

.*

.* will match as much as it can, but by default . .*将尽可能多地匹配,但默认情况下. doesn't match newlines. 与换行符不匹配。 If the text you're matching against has newlines and you're in MULTILINE but not DOTALL mode, then .*$ might not match where .* does. 如果您匹配的文本有换行符而您处于MULTILINE而不是 DOTALL模式,那么.*$可能与.*不匹配。 Without newlines (or if you're not in MULTILINE) or if you've set DOTALL, they're identical since * is a greedy operator and will match as much as it can. 没有换行符(或者如果你不在MULTILINE中)或者如果你设置了DOTALL,它们是相同的,因为*是一个贪婪的运算符,并且会尽可能多地匹配。

In the end though, the exact answer depends on the regular expression engine. 最后,确切的答案取决于正则表达式引擎。 So your results may differ. 所以你的结果可能不同。

并非总是如此,它取决于正在使用的设置,大多数正则表达式引擎都具有“多线”模式,如果启用它们,它们的行为将有所不同。

$ asserts that the match reaches the end of the string, which will always happen since . $断言匹配到达字符串的末尾,这将始终发生. matches anything. 匹配任何东西 So, yes, they are the same. 所以,是的,他们是一样的。

However, as Paul Creasey pointed out, there are times when they aren't the same. 然而,正如Paul Creasey指出的那样,有时它们不一样。 When multiline is enabled, $ will match the end of the multi-line string. 启用多行时, $将匹配多行字符串的结尾。 But, unless dot-all (meaning "dot" matches all) is also enabled, . 但是,除非dot-all(意为“dot”匹配all)也启用,否则. can't match newlines. 无法匹配换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM