正则表达式非连续字符 [英] Regex non-consecutive chars

查看:35
本文介绍了正则表达式非连续字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有:

[A-Za-z0-9._%+-]

这匹配任何包含字母、数字和某些特殊字符 (._%+-) 的字符串

This matches any string that contains letters, numbers, and certain special chars (._%+-)

如何更改它以使其不匹配连续包含特殊字符的字符串?

How can I change this so that it won't match a string that contains the special chars consecutively?

例如,我希望它匹配:foo.bar+testfoo.+bar+foo.

For example, I want it to match: foo.bar+test and foo.+bar and +foo.

但不是:foo..bar+testfoo.bar++testfoo.bar++

推荐答案

如果您的工具/语言支持前瞻,请尝试:

If your tool/language supports look aheads, try:

^(?!.*([._%+-])\1)[A-Za-z0-9._%+-]+$

这篇关于正则表达式非连续字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆