Regexp - start and end of line or string

C

Colin Bartlett

How often do people use \A and \z (match start and end of a string)
instead of ^ and $ (match start and end of a line within a string)?

This question is prompted by:

(1) part of a post in the "What are your ruby rough cuts ?" thread:
* Regexp ^ and $ work match more than just start and end of string.
For example, /^abc$/ does not match only "abc" but also "rm -rf /*\nabc"

Comment: using \A and \z seems to avoid the unwanted(?) matches.
Am I missing something?

(2) a Regexp used in the Find module
if File::ALT_SEPARATOR and file =~ /^(?:[\/\\]|[A-Za-z]:[\/\\]?)$/ then
which matches "start of a line" + X + "end of a line",
where X is one of / \\ C: C:/ C:\\

Comment: using ^ and $ in Find will match in the (admittedly rather
unlikely) situation of a file path string containing "\n/\n", and I'm
wondering why ^ and $ are used in this Find module regexp instead of
using \A and \z.
 
J

Justin Collins

How often do people use \A and \z (match start and end of a string)
instead of ^ and $ (match start and end of a line within a string)?

This question is prompted by:

(1) part of a post in the "What are your ruby rough cuts ?" thread:
* Regexp ^ and $ work match more than just start and end of string.
For example, /^abc$/ does not match only "abc" but also "rm -rf /*\nabc"

Comment: using \A and \z seems to avoid the unwanted(?) matches.
Am I missing something?

Most of the time I am going through data a line at a time, so this is
not a concern.

However, it seems to me that many people are not aware of this
distinction, thus we have things like this:
http://guides.rubyonrails.org/security.html#regular-expressions
(2) a Regexp used in the Find module
if File::ALT_SEPARATOR and file =~ /^(?:[\/\\]|[A-Za-z]:[\/\\]?)$/ then
which matches "start of a line" + X + "end of a line",
where X is one of / \\ C: C:/ C:\\

Comment: using ^ and $ in Find will match in the (admittedly rather
unlikely) situation of a file path string containing "\n/\n", and I'm
wondering why ^ and $ are used in this Find module regexp instead of
using \A and \z.

I suppose this is a bug.

-Justin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top