matching only alphabetic chars and only digits with periods

J

Jack

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
 
J

John W. Krahn

Jack said:
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
 
B

Brian Wakem

Jack said:
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}$/
 
D

Dr.Ruud

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
 
B

Ben Morrow

Quoth (e-mail address removed):
Jack said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top