I have got a text string like this:
test1test
I want to check if it contains at least one digit using a regex.
What would this regex look like?
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.
I’m surprised nobody has mentioned the simplest version:
This will match any digit. If your regular expression engine is Unicode-aware, this means it will match anything that’s defined as a digit in any language, not just the Arabic numerals 0-9.
There’s no need to put it in
[square brackets]to define it as a character class, as one of the other answers did;\dworks fine by itself.Since it’s not anchored with
^or$, it will match any subset of the string, so if the string contains at least one digit, this will match.And there’s no need for the added complexity of
+, since the goal is just to determine whether there’s at least one digit. If there’s at least one digit, this will match; and it will do so with a minimum of overhead.