simple regex not working

Discussion in 'Perl Misc' started by John, Sep 13, 2008.

  1. John

    John Guest

    Hi

    I'm trying to match a string beginning a letter A to R followed by any
    letter (from S to Z) or any digit.

    if ($x =~ /([A-Ra-r][S-Zs-z0-9]+)/) {$error=""; return}

    Well, it does not work.

    Any ideas?

    Regards
    John
     
    John, Sep 13, 2008
    #1
    1. Advertising

  2. John wrote:
    >
    > I'm trying to match a string beginning a letter A to R followed by any
    > letter (from S to Z) or any digit.
    >
    > if ($x =~ /([A-Ra-r][S-Zs-z0-9]+)/) {$error=""; return}
    >
    > Well, it does not work.
    >
    > Any ideas?


    You are not telling it to match only at the beginning of the string:

    if ( $x =~ /^([A-Ra-r][S-Zs-z0-9]+)/ ) {



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
     
    John W. Krahn, Sep 13, 2008
    #2
    1. Advertising

  3. John

    Dr.Ruud Guest

    John W. Krahn schreef:
    > John wrote:


    >> I'm trying to match a string beginning a letter A to R followed by
    >> any letter (from S to Z) or any digit.
    >> if ($x =~ /([A-Ra-r][S-Zs-z0-9]+)/) {$error=""; return}
    >> Well, it does not work.

    >
    > You are not telling it to match only at the beginning of the string:
    > if ( $x =~ /^([A-Ra-r][S-Zs-z0-9]+)/ ) {


    Also the capturing and the quantifier are likely not wanted:

    if ( $x =~ /^[A-Ra-r][S-Zs-z0-9]/ ) {$error=""; return}

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Sep 13, 2008
    #3
  4. John

    John Bokma Guest

    "John" <> wrote:

    > I'm trying to match a string beginning a letter A to R followed by any
    > letter (from S to Z) or any digit.
    >
    > if ($x =~ /([A-Ra-r][S-Zs-z0-9]+)/) {$error=""; return}


    if ($x =~ /^[A-R][S-Z\d]/i ) {

    $error = "";
    return;
    }

    --
    John http://johnbokma.com/ - Hacking & Hiking in Mexico

    Perl help in exchange for a gift:
    http://johnbokma.com/perl/help-in-exchange-for-a-gift.html
     
    John Bokma, Sep 14, 2008
    #4
  5. John

    Dr.Ruud Guest

    John Bokma schreef:
    > John:


    >> I'm trying to match a string beginning a letter A to R
    >> followed by any letter (from S to Z) or any digit.
    >>
    >> if ($x =~ /([A-Ra-r][S-Zs-z0-9]+)/) {$error=""; return}

    >
    > if ($x =~ /^[A-R][S-Z\d]/i ) {
    >
    > $error = "";
    > return;
    > }


    Ignoring the intricacies of /i and \d is not best practice, especially
    when it is so easy to avoid as in this case.

    Some reasons: lc() and uc() are not (always) symmetric, \d is
    (sometimes) a character set with 200+ different characters.

    If you mean [0-9], then you should write [0-9], not \d.

    There is a plan to make \d (at last) equivalent to only [0-9] (and \w to
    only [0-9A-Za-z_], etc.) in Perl 5.12, but that will break old code, so
    a new modifier like /U for non-Unicode semantics might be better.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Sep 14, 2008
    #5
  6. John

    John Bokma Guest

    "Dr.Ruud" <> wrote:

    > John Bokma schreef:
    >> John:

    >
    >>> I'm trying to match a string beginning a letter A to R
    >>> followed by any letter (from S to Z) or any digit.
    >>>
    >>> if ($x =~ /([A-Ra-r][S-Zs-z0-9]+)/) {$error=""; return}

    >>
    >> if ($x =~ /^[A-R][S-Z\d]/i ) {
    >>
    >> $error = "";
    >> return;
    >> }

    >
    > Ignoring the intricacies of /i and \d is not best practice, especially
    > when it is so easy to avoid as in this case.
    >
    > Some reasons: lc() and uc() are not (always) symmetric, \d is
    > (sometimes) a character set with 200+ different characters.
    >
    > If you mean [0-9], then you should write [0-9], not \d.


    Depends on your input data, no? Also, what does the OP mean with "any
    digit"...

    --
    John http://johnbokma.com/ - Hacking & Hiking in Mexico

    Perl help in exchange for a gift:
    http://johnbokma.com/perl/help-in-exchange-for-a-gift.html
     
    John Bokma, Sep 14, 2008
    #6
    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. tshad

    Regex not working in page_load

    tshad, Apr 18, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    496
    tshad
    Apr 18, 2005
  2. Replies:
    3
    Views:
    810
    Reedick, Andrew
    Jul 1, 2008
  3. Gabriel Rossetti
    Replies:
    0
    Views:
    614
    Gabriel Rossetti
    Mar 31, 2009
  4. Jacob N.
    Replies:
    0
    Views:
    99
    Jacob N.
    Dec 29, 2010
  5. Replies:
    3
    Views:
    169
    Paul Lalli
    Oct 27, 2005
Loading...

Share This Page