开发者

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 开发者_Go百科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. 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.

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. When multiline is enabled, $ will match the end of the multi-line string. But, unless dot-all (meaning "dot" matches all) is also enabled, . can't match newlines.


Not always, it depends on the settings that are being used, most regex engines have a "multiline" mode, if that is enabled, they will behave differently.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜