Regex for upper and lower+numbers or other non-alphabetic
See my post here
Regex for at least 8 + upper and lower+numbers or other non-alphabetic
Suppose i just need this 2 condition below, what should be the regular expression?
- Contains upper and lower 开发者_StackOverflowcase letters.
- Contains numbers or other non-alphabetic characters.
I tried ^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[^a-zA-Z])$
but not working
BUT just-
-(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[^a-zA-Z])
is working
can i use just (?=.*?[a-z])(?=.*?[A-Z])(?=.*?[^a-zA-Z])
for the purpose?
The problem here is that the look ahead/behind are zero-width, "non-consuming". That means your
^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[^a-zA-Z])$
will never match because the lookarounds consumed nothing and whats then left? only ^$
that would match an empty string, but this does not meet your lookahead criterias.
So you should use
^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[^a-zA-Z]).*$
that .*
will consume everything and the look aheads ensure the criterias.
you can also define a length replacing the *
with {x,}
where x is the minimum amount of characters of the string
^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[^a-zA-Z])$
this means: start of string followed by anything containing lowercase or uppercase or non-alphabetic, but the next position must be end of string - this can not happen.
Your accepted aswer in your previous question is OK, is there any problem? Here you can read something more about assertions in regex http://cz.php.net/manual/en/regexp.reference.assertions.php
精彩评论