Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Regexp - start and end of line or string

Reply
Thread Tools

Regexp - start and end of line or string

 
 
Colin Bartlett
Guest
Posts: n/a
 
      01-16-2011
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.

 
Reply With Quote
 
 
 
 
Justin Collins
Guest
Posts: n/a
 
      01-16-2011
On 01/15/2011 06:43 PM, Colin Bartlett wrote:
> 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/securi...ar-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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regexp matching end of line or comma Jean-Michel Pichavant Python 1 11-25-2010 06:32 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Striping spaces from the beginning and end of a string through RegExp romayankin@gmail.com Javascript 3 07-17-2006 04:05 AM
Specify start and length, beside start and end, in slices Noam Raphael Python 17 05-26-2004 09:30 PM



Advertisments