How can I create a regex expression which will match only letters and numbers, and one space between each word?
Good Examples:
Amazing Hello Beautiful World I am 13 years old
Bad Examples:
Hello world I am 13 years old. I am Chuck Norris
How can I create a regex expression which will match only letters and numbers, and one space between each word?
Good Examples:
Amazing Hello Beautiful World I am 13 years old
Bad Examples:
Hello world I am 13 years old. I am Chuck Norris
Most regex implementations support named character classes:
^[[:alnum:]]+( [[:alnum:]]+)*$
You could be clever though a little less clear and simplify this to:
^([[:alnum:]]+ ?)*$
FYI, the second one allows a spurious space character at the end of the string. If you don't want that stick with the first regex.
Also as other posters said, if [[:alnum:]]
doesn't work for you then you can use [A-Za-z0-9]
instead.