Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search Patterns 1

Status
Not open for further replies.

BenRussell

Programmer
Mar 12, 2001
243
US
Im working on a script but I need to filter out all non-letters/numbers from the form values. How would I make a statement to say the following in perl code "if $Variable1 is a value that contains ONLY letters and numbers $Variable2 = Yes"? - Ben Russell
- President of Intracor Technologies (
 
Code:
if ( $Variable1 =~ /\A[A-Za-a0-9]*\Z/ ) {
   $Variable2 = "Yes";
} else {
   $Variable2 = "No";
}
Note that this doesn't include spaces either, just uppercase and lowercase letters and numbers.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
OOPS! That should have been:
Code:
/\A[A-Za-z0-9]*\Z/
"Beware the odd typo," as one of our more popular members says. Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
To add any other characters just put them between the square brackets with the A-Za-z0-9. Remember to escape special characters, and don't put the ^ as the first character after the [ (it will negate the character class). For example to add spaces, commas and periods, change the class to:
Code:
/\A[A-Za-z0-9 \.,]*\Z/
Note that you have to escape the period.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top