matching only alphabetic chars and only digits with periods

Discussion in 'Perl Misc' started by Jack, Jul 14, 2006.

  1. Jack

    Jack Guest

    Hi I am trying to match ONLY alphabetic chars (not digits or anything
    else)..

    I am finding that

    $temp = '14.5a';
    if ($temp =~ m/\D+/ and $temp !~ m/\d+/) { print " alpha "; }

    Only matches NONDIGITS... ? # . is a non digit but not alphabetic..
    how do I test for pure alphabetic ?

    Likewise, how do I test for pure numeric dollar amount with a "period"
    in them "."- the following doesnt work since the Not equal excludes "."
    off the bat..
    $temp = '14.5a';
    if ($temp =~ m/\d+/ and $temp !~ m/\D+/ and $temp =~ m/\.{1}/) { print
    " digit DOT "; }

    Any slick ways to change this specific code and keep it simple ?
    Thank you,
    Jack
     
    Jack, Jul 14, 2006
    #1
    1. Advertising

  2. Jack wrote:
    > Hi I am trying to match ONLY alphabetic chars (not digits or anything
    > else)..


    $ perl -le'print "a3#b7&C9(D.>" =~ /[[:alpha:]]/g'
    abCD
    $ perl -le'print "a3#b7&C9(D.>" =~ /[^\W\d_]/g'
    abCD



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Jul 14, 2006
    #2
    1. Advertising

  3. Jack

    Brian Wakem Guest

    Jack wrote:

    > Hi I am trying to match ONLY alphabetic chars (not digits or anything
    > else)..
    >
    > I am finding that
    >
    > $temp = '14.5a';
    > if ($temp =~ m/\D+/ and $temp !~ m/\d+/) { print " alpha "; }
    >
    > Only matches NONDIGITS... ? # . is a non digit but not alphabetic..
    > how do I test for pure alphabetic ?



    m/^[a-zA-Z]+$/


    > Likewise, how do I test for pure numeric dollar amount with a "period"
    > in them "."- the following doesnt work since the Not equal excludes "."
    > off the bat..
    > $temp = '14.5a';
    > if ($temp =~ m/\d+/ and $temp !~ m/\D+/ and $temp =~ m/\.{1}/) { print
    > " digit DOT "; }



    m/^\d+\.\d{2}$/


    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
     
    Brian Wakem, Jul 14, 2006
    #3
  4. Jack

    Dr.Ruud Guest

    Jack schreef:

    > I am trying to match ONLY alphabetic chars [...]
    >
    > $temp = '14.5a';
    > if ($temp =~ m/\D+/ and $temp !~ m/\d+/) { print " alpha "; }
    >
    > Only matches NONDIGITS... ? # . is a non digit but not
    > alphabetic.. how do I test for pure alphabetic ?


    You should get to know anchors, like ^and $, or \A and \z. See `perldoc
    perlre`.

    The regex
    /\A [[:alpha:]]+ \z/x

    will only match non-empty strings with only alphabetic characters.


    > Likewise, how do I test for pure numeric dollar amount with a "period"
    > in them "."


    Check out Regexp::Common.
    http://search.cpan.org/search?module=Regexp::Common

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jul 14, 2006
    #4
  5. Jack

    Ben Morrow Guest

    Quoth :
    > Jack wrote:
    >
    > > Hi I am trying to match ONLY alphabetic chars (not digits or anything
    > > else)..
    > >
    > > I am finding that
    > >
    > > $temp = '14.5a';
    > > if ($temp =~ m/\D+/ and $temp !~ m/\d+/) { print " alpha "; }
    > >
    > > Only matches NONDIGITS... ? # . is a non digit but not alphabetic..
    > > how do I test for pure alphabetic ?

    >
    > m/^[a-zA-Z]+$/


    No! You will miss all locale-specific and Unicode alphabetics, and it
    won't work at all on non-ASCII machines. Just say what you mean:

    /[[:alpha:]]/

    Ben

    --
    Joy and Woe are woven fine,
    A Clothing for the Soul divine William Blake
    Under every grief and pine 'Auguries of Innocence'
    Runs a joy with silken twine.
     
    Ben Morrow, Jul 14, 2006
    #5
    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. =?Utf-8?B?VGltOjouLg==?=

    MAJOR problem with alphabetic paging

    =?Utf-8?B?VGltOjouLg==?=, Jul 26, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    504
    =?Utf-8?B?VGltOjouLg==?=
    Jul 26, 2005
  2. steve

    list in alphabetic order

    steve, Sep 26, 2004, in forum: HTML
    Replies:
    7
    Views:
    623
    steve
    Sep 27, 2004
  3. Hex digits to special chars

    , Aug 1, 2006, in forum: C Programming
    Replies:
    8
    Views:
    342
    Herbert Rosenau
    Aug 4, 2006
  4. py_genetic
    Replies:
    6
    Views:
    334
    py_genetic
    Jun 19, 2007
  5. Sandhya Prabhakaran
    Replies:
    6
    Views:
    611
    alex23
    Aug 3, 2009
Loading...

Share This Page