4.8. Limit Input to Alphanumeric Characters
Problem
Your application requires that users limit their responses to one or more alphanumeric English characters (letters A–Z and a–z, and digits 0–9).
Solution
With regular expressions at your disposal, the solution is dead simple. A character class can set up the allowed range of characters. With an added quantifier that repeats the character class one or more times, and anchors that bind the match to the start and end of the string, you’re good to go.
Regular expression
^[A-Z0-9]+$
| Regex options: Case insensitive |
| Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby |
Ruby example
if subject =~ /^[A-Z0-9]+$/i
puts "Subject is alphanumeric"
else
puts "Subject is not alphanumeric"
endFollow Recipe 3.6 to add this regex to your code in other programming languages. Recipe 3.4 shows how to set regular expression options, including the “case insensitive” modifier used here.
Discussion
Let’s look at the four pieces of this regular expression one at a time:
^ # Assert position at the beginning of the string. [A-Z0-9] # Match a character from A to Z or from 0 to 9 + # between one and unlimited times. $ # Assert position at the end of the string.
| Regex options: Case insensitive, free-spacing |
| Regex flavors: .NET, Java, XRegExp, PCRE, Perl, Python, Ruby |
The ‹^› and
‹$› assertions at the beginning and end of the regular expression ensure that the entire input string is tested. Without them, the regex could match any part of a longer string, letting invalid ...