棘手的正则表达式
我只需要允许 0-25 个字符长度的字母数字字符(大写),并且不允许延迟所有重复的数值。
我有第一部分: Regex.IsMatch(tmpResult, "^[0-9A-Z]{0,25}$"); (很简单)
111112 - 匹配
AABD333434 - 匹配
55555555 - 不匹配
555 - 不匹配
有人可以帮我解决这个问题吗?
I need to allow only alphanumeric characters (with uppercase) from 0-25 chars length and no lazy all-repetition numeric value.
I've got the first part: Regex.IsMatch(tmpResult, "^[0-9A-Z]{0,25}$"); (that's easy)
111112 - match
AABD333434 - match
55555555 - no match
555 - no match
Could anyone please help me with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
额外的
(?!(.)\1*$)
将拒绝任何由重复相同字符组成的字符串。(?!…)
是一个负向预测,如果…
匹配,则将导致主正则表达式失败,并且( .)\1*
将匹配重复字符的字符串。The extra
(?!(.)\1*$)
will reject any strings that is composed of repeating same character.The
(?!…)
is a negative lookahead that will cause the primary regex fail if the…
is matched, and the(.)\1*
will match a string of repeating characters.您可以使用普通方法来完成它...一旦您让它与您的第一个表达式匹配,只需使用子例程来迭代每个字符,并在第一次遇到与字符串中第一个字符不同的字符时返回 true 。
在仅检查 大多数 字符串的前 2 个字符后,它应该返回 true,除非它是无效字符串。
如果实现得当,这应该与正则表达式一样快,甚至更快。
You could just do it using a normal method... Once you have it match your first expression there, just use a subroutine to iterate through each character and return true the first time you encounter a character that differs from the first in the string.
It should return true after checking only the first 2 characters for most strings, unless it's an invalid string.
This should be equally as fast as a regex if not faster, if it is well implemented.