Regex help with specific char
I recently got an answer to a question: Consecutive Regex
But now开发者_运维百科 I want to match only if there is an @ symbol in there.
The original regex is:
(?!.*([._%+-])\1)[A-Za-z0-9._%+-]+
So I tried:
(?!.*([._%+-])\1)[A-Za-z0-9._%+-]+@(?!.*([._%+-])\1)[A-Za-z0-9._%+-]+
But this doesn't allow any of my special chars to follow the @ symbol.
For instance, the above matches +foo.bar+-test+@m
but not +foo.bar+-test+@m+
Any ideas of what I'm doing wrong here?
^(?!.*([._%+-])\1)(?=.*@)[\w.%+@-]+$
You have reused backreference \1
, therefore it stops you from matching the "special" char again. You don't need to repeat the lookahead assertion at all. Try
(?!.*([._%+-])\1)[A-Za-z0-9._%+-]+@[A-Za-z0-9._%+-]+
精彩评论