4.8. Limit Input to Alphanumeric Characters
Problem
Your application requires that users limit their responses to one or more alphanumeric characters from the English alphabet.
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
if subject =~ /^[A-Z0-9]+$/i
puts "Subject is alphanumeric"
else
puts "Subject is not alphanumeric"
endDiscussion
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, 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 characters through. The plus
quantifier ‹+› repeats the preceding element one or ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access