CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
pattern matching only alphabetic characters

 



Radtherapist
Novice

Apr 2, 2009, 5:20 PM

Post #1 of 8 (3606 views)
pattern matching only alphabetic characters Can't Post

I am trying to match only alphabetic characters, but I can't work out the expression to use in the match.

I have tried:

\b\D\b,

\b\D*\b

\b[a-z][A-Z]\b

plus a few others. But when I try to match something like "aa3" I get a match. I can see that it would pick up the a's, and therefore match. How does one make it not match if there is a number or special character in the mix?

I know there is an OR symbol (|), but is there an AND symbol. My thoughts are if I can do something like [^0-9] AND ([a-z]|[A-Z]), that this might work. Any comments are appreciated.

Regards

Vincent


KevinR
Veteran


Apr 2, 2009, 7:43 PM

Post #2 of 8 (3604 views)
Re: [Radtherapist] pattern matching only alphabetic characters [In reply to] Can't Post

Your explanation is a bit all over the place. If you want to match alpha-numeric stuff you can use \w which is a short cut character class for: a-zA-Z0-9_ or you can use: [a-zA-Z0-9] if you don't want the underscore included. Some sample data might help and a clearer explanation of what you are trying to match.
-------------------------------------------------


Radtherapist
Novice

Apr 2, 2009, 9:02 PM

Post #3 of 8 (3602 views)
Re: [KevinR] pattern matching only alphabetic characters [In reply to] Can't Post

Hi,

I was after something to test this:

does aaabbb9 only have only alphabet characters - no numbers - no match

BBBAAA - match

aA7Ba -no match

5aaaBBB - no match

I am wanting to ensure a string of characters does not contain numbers.

Vince


Tumata
User


Apr 2, 2009, 11:07 PM

Post #4 of 8 (3596 views)
Re: [Radtherapist] pattern matching only alphabetic characters [In reply to] Can't Post

Hi, Vince. Try this script:


Code
#!/usr/bin/perl 

local $_ = "aA3Bb";

if ( m/\d/ ) {
print 'Conntains numbers';
} else {
print 'Doesn\'t contain numbers';
}



Radtherapist
Novice

Apr 3, 2009, 1:26 AM

Post #5 of 8 (3590 views)
Re: [Tumata] pattern matching only alphabetic characters [In reply to] Can't Post

Thanks Tumata,

I can see this would work.

Does this do the same sort of thing, but only looking for the alphabet characters only instead of numeric:

^[a-zA-Z]*$

Vince


Tumata
User


Apr 3, 2009, 1:53 AM

Post #6 of 8 (3589 views)
Re: [Radtherapist] pattern matching only alphabetic characters [In reply to] Can't Post

Yes. All strings that contains only letters will match this pattern. Empty strings will also match Smile


KevinR
Veteran


Apr 3, 2009, 9:48 AM

Post #7 of 8 (3580 views)
Re: [Radtherapist] pattern matching only alphabetic characters [In reply to] Can't Post


In Reply To
Thanks Tumata,

I can see this would work.

Does this do the same sort of thing, but only looking for the alphabet characters only instead of numeric:

^[a-zA-Z]*$

Vince


It does the same thing but Tumatas code would be more efficient. Keep in mind, as he said, yours, and his, can allow empty strings to match successfully. If you don't want that behavior change the * quantifier to + in your regexp.
-------------------------------------------------


Radtherapist
Novice

Apr 3, 2009, 7:44 PM

Post #8 of 8 (3576 views)
Re: [KevinR] pattern matching only alphabetic characters [In reply to] Can't Post

Many thanks to both.

Vince

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives