Very simple regex question

Discussion in 'Ruby' started by Geometric Patterns, Nov 13, 2010.

  1. Hi, I have a very simple regex question.

    I don't understand why the regex in the example below isn't identifying
    that the string is longer than 12 characters long...

    >> line = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

    => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    >> if line =~ /[a-zA-Z]{2,12}/
    >> puts "good"
    >> else

    ?> puts "bad"
    >> end

    good
    => nil
    >>


    Any ideas?

    Many thanks in advance!

    --
    Posted via http://www.ruby-forum.com/.
     
    Geometric Patterns, Nov 13, 2010
    #1
    1. Advertising

  2. Geometric Patterns

    Jeremy Bopp Guest

    On 11/13/2010 01:06 PM, Geometric Patterns wrote:
    > Hi, I have a very simple regex question.
    >
    > I don't understand why the regex in the example below isn't identifying
    > that the string is longer than 12 characters long...
    >
    >>> line = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

    > => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    >>> if line =~ /[a-zA-Z]{2,12}/
    >>> puts "good"
    >>> else

    > ?> puts "bad"
    >>> end

    > good
    > => nil
    >>>

    >
    > Any ideas?


    Your regexp will match anywhere within the string where there is a run
    of 2 to 12 characters. If you want to ensure that there is nothing
    before or after whatever is matched, you need to use the begin and end
    of string anchors as follows when you define your regexp:

    /\A[a-zA-Z]{2,12}\z/

    -Jeremy
     
    Jeremy Bopp, Nov 13, 2010
    #2
    1. Advertising

  3. On Sat, Nov 13, 2010 at 11:06 AM, Geometric Patterns
    <> wrote:

    > I don't understand why the regex in the example below isn't identifying
    > that the string is longer than 12 characters long...


    >>> if line =~ /[a-zA-Z]{2,12}/


    That succeeds if the string *contains* between 2 and 12 characters,
    not if it consists solely of 2-12 characters.

    line = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    if line =~ /[a-zA-Z]{2,12}/
    puts "good" # you'll get this
    else
    puts "bad"
    end

    if line =~ /\A[a-zA-Z]{2,12}\Z/
    puts "good"
    else
    puts "bad" # you'll get this
    end

    FWIW,
    --
    Hassan Schroeder ------------------------
    twitter: @hassan
     
    Hassan Schroeder, Nov 13, 2010
    #3
    1. Advertising

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. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    510
    Raymond Hettinger
    Jul 27, 2003
  2. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,704
    Merrill & Michele
    Oct 19, 2004
  3. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    453
    jacob navia
    Oct 17, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    534
    Dave Thompson
    Feb 14, 2005
  5. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,355
Loading...

Share This Page