正则表达式不允许双下划线 [英] Regex not to allow double underscores
本文介绍了正则表达式不允许双下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试应用正则表达式以不允许带有双下划线的字符串
Trying to apply regex for not allowing a string with double underscores
[a-z][a-z0-9_-]+[^__]
但它在许多情况下都失败了,例如:
but its failing in many cases like:
ab_ doesn't matches whereas it should
ab__c_ matches whereas it shouldn't.
推荐答案
[^__]
匹配一个不是下划线的字符.要断言您的字符串没有两个连续的下划线,您可以使用 负前瞻:
[^__]
matches one character that is not underscore. To assert that your string doesn't have two consecutive underscores, you could use a negative lookahead:
^(?!.*__.*)[a-z][a-z0-9_-]+$
环视断言您的字符串没有有两个连续的下划线(?!.*__.*)
,然后如果断言没有匹配您所需的字符串失败 -- [az][a-z0-9_-]+
.
The lookaround asserts that your string does not have two consecutive underscores (?!.*__.*)
, and then matches your required string if the assertion does not fail -- [a-z][a-z0-9_-]+
.
这篇关于正则表达式不允许双下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文