Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > matching only alphabetic chars and only digits with periods

Reply
Thread Tools

matching only alphabetic chars and only digits with periods

 
 
Jack
Guest
Posts: n/a
 
      07-14-2006
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

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      07-14-2006
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
 
Reply With Quote
 
 
 
 
Brian Wakem
Guest
Posts: n/a
 
      07-14-2006
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
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      07-14-2006
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."


 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      07-14-2006

Quoth http://www.velocityreviews.com/forums/(E-Mail Removed):
> 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. (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract the numeric and alphabetic part from an alphanumeric string Sandhya Prabhakaran Python 6 08-03-2009 04:40 PM
How to truncate char string fromt beginning and replace chars instring by other chars in C or C++? Hongyu C++ 9 08-08-2008 12:18 PM
Hex digits to special chars abhi147@gmail.com C Programming 8 08-04-2006 05:37 AM
Imaging a failing hard drive in pieces, because the drive only works for short periods of time Keefe8@gmail.com Computer Support 22 01-09-2006 03:21 PM
Floats to chars and chars to floats Kosio C Programming 44 09-23-2005 09:49 AM



Advertisments