Regexp - start and end of line or string

Discussion in 'Ruby' started by Colin Bartlett, Jan 16, 2011.

  1. 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.
     
    Colin Bartlett, Jan 16, 2011
    #1
    1. Advertisements

  2. 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/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
     
    Justin Collins, Jan 16, 2011
    #2
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noam Raphael
    Replies:
    17
    Views:
    754
    Noam Raphael
    May 26, 2004
  2. Jean-Michel Pichavant

    regexp matching end of line or comma

    Jean-Michel Pichavant, Nov 25, 2010, in forum: Python
    Replies:
    1
    Views:
    425
    Saul Spatz
    Nov 25, 2010
  3. albert kao
    Replies:
    3
    Views:
    2,505
    Arne Vajhøj
    Feb 2, 2011
  4. Mikel Lindsaar
    Replies:
    0
    Views:
    749
    Mikel Lindsaar
    Mar 31, 2008
  5. Joao Silva
    Replies:
    16
    Views:
    681
    7stud --
    Aug 21, 2009
  6. Replies:
    6
    Views:
    147
    John Bokma
    Jul 19, 2006
  7. Replies:
    3
    Views:
    197
    Randy Webb
    Jul 17, 2006
  8. charliefortune

    find start and end of highlighted text in string

    charliefortune, Feb 15, 2007, in forum: Javascript
    Replies:
    1
    Views:
    168
    Une Bévue
    Feb 15, 2007
Loading...