Regular expression check for special character or number
I have such regular expression which checked for at least one special character
in the string:
^(.*[^0-9a-zA-Z].*)$
But how could i change this one to check for at l开发者_如何转开发east one special character
or at leas one number
in the string?
.*[^a-zA-Z]+.*
would match anything followed by a special character followed by anything.
Notice that I just removed the 0-9
from the character class (characters included in the square brackets).
Also, I removed the ^
and $
markers -- those match the beginning and end of string respectively. You don't need it because you're making it redundant with the .*
(match zero or more of any character) anyway.
In fact, if you're just checking if the string contains a special character, then the following is good enough:
[^a-zA-Z]
you can use the Expresso, it is a smart tool for generate RegExps Expresso
精彩评论