开发者

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._%+-]+
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜