Complex(?) regex:是表达式,但不是另一个
(如果你能做出更好的标题,请这样做)
嗨,
我需要确保一个字符串与以下正则表达式匹配:
^[0-9a-zA-Z]{1}[0-9a-zA-Z\.\-_]*$
(Starts with a letter or number, then any number of letters, numbers, dots, dashes or underscores)
但考虑到这一点,我需要确保它与 Guid 不匹配,我的 Guid 匹配正则表达式看起来像这样(显然,这需要在合并结果中被否定):
^([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}$
这里的最后一个要求是它们必须(如果可能的话)合并到单个表达式中。
(If you can make a better title, please do)
Hi,
I need to make sure a string matches the following regex:
^[0-9a-zA-Z]{1}[0-9a-zA-Z\.\-_]*$
(Starts with a letter or number, then any number of letters, numbers, dots, dashes or underscores)
But given that, I need to make sure it doesn't match a Guid, my Guid matching reg-ex looks like this (obviously, this needs to be negated in the merged result):
^([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}$
The last requirement here is that they must (if it's possible) be merged into a single expression.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以只使用否定先行断言。
You can just use a negative lookahead assertion.
如果您的语言支持,最简单的方法是使用 否定前瞻:
The simplest way to do this, if your language supports it, is to use a negative lookahead:
我建议使用这两个正则表达式而不是组合它们。在大多数情况下,这将更容易维护并且更快。像这样的东西:
I would suggest using both regular expressions rather than combining them. This will be easier to maintain as well as faster in most cases. Something like this: