I need a regular expression to allow the user to enter an alphanumeric string that starts with a letter (not a digit).
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This should work in any of the Regular Expression (RE) engines. There is a nicer syntax in the PCRE world but I prefer mine to be able to run anywhere:
Basically, the first character must be alpha, followed by zero or more alpha-numerics. The start and end tags are there to ensure that the whole line is matched. Without those, you may match the
AB12of the"@@@AB12!!!"string.Full explanation:
Update:
As Richard Szalay rightly points out, this is ASCII only (or, more correctly, any encoding scheme where the A-Z, a-z and 0-9 groups are contiguous) and only for the “English” letters.
If you want true internationalized REs (only you know whether that is a requirement), you’ll need to use one of the more appropriate RE engines, such as the PCRE mentioned above, and ensure it’s compiled for Unicode mode. Then you can use “characters” such as
\p{L}and\p{N}for letters and numerics respectively. I think the RE in that case would be:but I’m not certain. I’ve never used REs for our internationalized software. See here for more than you ever wanted to know about PCRE.