1

I need a regex that matches the alphanumeric patterns except totally numeric ones.

asdfgesod valid
1asdndwdd valid
asd124asd valid
a2asd43bd valid
123346678 invalid
0

4 Answers 4

4
/^[a-z0-9]*[a-z][a-z0-9]*$/i

Broken down:

^[a-z0-9]* - String starts with any number (including zero) of alphanumeric characters

[a-z] - String has an a-z character

[a-z0-9]*$ - String ends with any number (including zero) of alphanumeric characters

Sign up to request clarification or add additional context in comments.

4 Comments

As you can see the first four match and the fifth doesn't as intended: codepad.viper-7.com/HiTFio
I feel silly when the answer is that easy. I tried lot of ridiculous things. Thank you so much. You saved me from endless headaches.
@tuze You're welcome. Also don't include the i at the end if you don't want (uppercase) A-Z to be considered alpha characters.
Thanks. I know how to use modifiers.
0

You can try:

/^(?![0-9]+$)[a-z0-9]+$/i

See it

[a-z0-9]+$ ensures the string is made of only alphanumeric characters.

The negative lookahead (?![0-9]+$) ensures the string is not all numeric.

Comments

0

A solution would be to first check if the string contains only digits, if not check rest of the string. The following regex would do that:

^(?![\d]+$)[a-z0-9]+$

See it in action here: http://regexr.com?2v8n5

Comments

0

Use in case of java StringUtils.isNumeric(String value) check if string contains only numbers.

BTW: if string matches regex: "^\d*$" this means that it contains only digits.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.