Gossamer Forum
Home : General : Perl Programming :

Detect alphabet input

Quote Reply
Detect alphabet input
Hello,

How can I make an input string come out to be ALPHABET ONLY ( a,b,c,...x,y, and z ONLY) (maybe the numbers from 0-9 too) and return error message if it has ANY weird character : such as: $ & ` % ^ ~ / \ = ...

Thank you all in advance!
Quote Reply
Re: Detect alphabet input In reply to
I think this will work.

Code:
unless ($test =~ /^(\w|\s)+$/) {
print "error";
}

The above will also allow spaces. If you don't want spaces either, use

Code:
unless ($test =~ /^\w+$/) {
print "error";
}


------------------
JPD





Quote Reply
Re: Detect alphabet input In reply to
Hi Carol,

Any chance of you explaining why? Doesn't matter if you don't have the time or just picked it up from somewhere, but I reckon that's why I'm having problems with regular expressions, I need them explained to me.

Cheers,
adam
Quote Reply
Re: Detect alphabet input In reply to
You want an explanation of the regular expression? Well, I'm not too handy with them either, but I'll give it a shot.

The \w means "any word character" -- which is any letter of the alphabet or digit.

The \s means "any whitespace character" -- which is spaces and carriage returns.

The | means "or."

The ^ means "starts with" and the $ means "ends with." The "+" means -- well, I'm sure exactly what it means, but I know it works. Smile It has to do with the middle, anyway.

What happens when you use "^\w+$" is that it will only accept word characters. The other one here -- "^(\w|\s)+$" -- only accepts word or whitespace characters. You can do the same thing with digits -- "^\d+$". You can probably also do it with specific letters, but I haven't tested that out yet. I'm guessing here, but to test whether a string consisted of nothing but vowels, for example, you could probably use "^(a|e|i|o|u)+$".

All of this is trial and error -- lots of trials, most of which end in error. Smile I usually write little Perl scripts and test them on my home computer to see if the syntax is right.

------------------
JPD





Quote Reply
Re: Detect alphabet input In reply to
 
Excellent, that's a nice start to my tuition. Thanks Carol, yer a star. Now, how much do I owe you? Just charge it to my usual account. Smile

adam
Quote Reply
Re: Detect alphabet input In reply to
Here's a start: http://www.codebits.com/p5be/ch10.cfm . I'd also highly recommend picking up a copy of Mastering Regular Expressions, by Jeffery Freidl, available from O'Rielly & Associates.

--mark
Quote Reply
Re: Detect alphabet input In reply to
You're welcome, Adam.

Thank you, Mark. I've been looking for a page like that, but I guess the search engines I tried missed it. I've got it bookmarked now, though.

"Mastering Regular Expressions" is the next book on my to-buy list.

------------------
JPD





Quote Reply
Re: Detect alphabet input In reply to
Hate to nitpick, but:

Quote:
The \w means "any word character" -- which is any letter of the alphabet or digit.

or underscore. =)

Cheers,

Alex

Quote Reply
Re: Detect alphabet input In reply to
Caught me again, Alex. Smile Yep, that ol' underscore is there, too. So if he didn't want the underscore or spaces, it would be

/^[a-zA-Z0-9]+$/

Right?

And if he did want spaces, just add a space within the brackets?

/^[a-zA-Z0-9 ]+$/

(I'm gonna get these things figured out one o' these days!)


------------------
JPD





Quote Reply
Re: Detect alphabet input In reply to
Looks right! I generally use \s for spaces instead of just a space, but either should work.

Cheers,

Alex
Quote Reply
Re: Detect alphabet input In reply to
But doesn't \s also allow \n and some other characters? If he didn't want a newline to be added, he'd have to specifically mention the space by itself. (I'm looking at "Learning Perl" now and it says that \s covers
[space],\n,\t,\r,\f.)

Now am I nit-picking? Wink


------------------
JPD