开发者

Why do Ruby's regular expressions use \A and \z instead of ^ and $?

I'm not a Ruby programmer, but as I was reading through the extensive Ruby on Rails security guide, I noticed this section:

A common pitfall in Ruby’s regular expressions is to match the string’s beginning and end by ^ and $, instead of \A and \z.

Does anyone know if this is this just a matter of aesthetics or something else? I ask because I've only used language开发者_JS百科s that use ^ and $.


This isn't specific to Ruby; \A and \Z are not the same thing as ^ and $. ^ and $ are the start and end of line anchors, whereas \A and \Z are the start and end of string anchors.

Ruby differs from other languages in that it automatically uses "multiline mode" (which enables the aforementioned behaviour of having ^ and $ match per line) for regular expressions, but in most other flavours you need to enable it yourself, which is probably why that article contains the warning.

Reference: http://www.regular-expressions.info/anchors.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜