What am I missing in perlre

Discussion in 'Perl Misc' started by janfure@gmail.com, Jun 15, 2006.

  1. Guest

    I wrote a script for detecting alphabetical/non numeric characters:

    #!/usr/local/bin/perl -w
    use strict;
    $_ = '2 3';
    if ($_ =~ m/\D/x){
    print "$_ has non numerical character(s)!\n";
    }
    $_ = '2A 3';
    if ($_ =~ m/\D/x){
    print "$_ has non numerical character(s)!\n";
    }

    This script does not do what I expected, the presence of the space in
    the string seems to cause a match with /\D/x, despite the 'x' modifier,
    which causes the regex engine to ignore MOST whitespace, but maybe not
    the one that mattered to me.

    This script (stripping out spaces from $_) does what I expected the
    first one to do:

    #!/usr/local/bin/perl -w
    use strict;
    $_ = '2 3';
    $_ =~ s/\s+//g;
    if ($_ =~ m/\D/){
    print "$_ has non numerical character(s)!\n";
    }
    $_ = '2A 3';
    $_ =~ s/\s+//g;
    if ($_ =~ m/\D/){
    print "$_ has non numerical character(s)!\n";
    }

    What am I missing, is there a better way to match alphabetical letters
    than /\D/x?

    Jan
    , Jun 15, 2006
    #1
    1. Advertising

  2. writes:

    > I wrote a script for detecting alphabetical/non numeric characters:
    >
    > #!/usr/local/bin/perl -w
    > use strict;
    > $_ = '2 3';
    > if ($_ =~ m/\D/x){
    > print "$_ has non numerical character(s)!\n";
    > }
    > $_ = '2A 3';
    > if ($_ =~ m/\D/x){
    > print "$_ has non numerical character(s)!\n";
    > }
    >
    > This script does not do what I expected, the presence of the space in
    > the string seems to cause a match with /\D/x, despite the 'x' modifier,
    > which causes the regex engine to ignore MOST whitespace


    The 'x' modifier ignores whitespace and comments *in the pattern*, not in
    the target string. It allows you to write stuff like this:

    m/
    \w # One alphanumeric character
    \d* # Followed by zero or more digits
    /x;

    > What am I missing, is there a better way to match alphabetical letters
    > than /\D/x?


    m/[[:alpha:]]/;

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Jun 15, 2006
    #2
    1. Advertising

  3. <> wrote:

    > #!/usr/local/bin/perl -w


    use warnings; # the modern way, much better than -w



    > if ($_ =~ m/\D/x){


    > This script does not do what I expected, the presence of the space in
    > the string seems to cause a match with /\D/x,



    Yes, because space _is_ a non-digit character.


    > despite the 'x' modifier,
    > which causes the regex engine to ignore MOST whitespace,



    *in the pattern*. (not in the string that the pattern is to match against)

    spaces are ignored in the _pattern_

    m/ \D /x

    is equivalent to writing the pattern the way you originally did.

    The m//x modifier does not change the meaning of any of
    the regex backslash escape shortcuts.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 15, 2006
    #3
  4. Guest

    Sherm Pendley wrote:

    > m/[[:alpha:]]/;
    >
    > sherm--
    >

    -----------------------<SNIP>---------------------------
    Thanks, that did the job.

    Jan
    , Jun 20, 2006
    #4
    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. Paul Fox
    Replies:
    0
    Views:
    744
    Paul Fox
    Mar 5, 2004
  2. Mark Newby
    Replies:
    1
    Views:
    1,696
  3. Gary Herron
    Replies:
    2
    Views:
    662
    Bruno Desthuilliers
    Jul 4, 2006
  4. Xah Lee

    perldoc incompetence: perlre /s

    Xah Lee, Apr 12, 2004, in forum: Perl Misc
    Replies:
    18
    Views:
    304
  5. Kevin Crosbie

    Boolean Regexp with perlre

    Kevin Crosbie, Sep 13, 2006, in forum: Perl Misc
    Replies:
    12
    Views:
    165
    Uri Guttman
    Sep 14, 2006
Loading...

Share This Page