不允许连续点的正则表达式 [英] Regex that does not allow consecutive dots

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

问题描述

我有一个正则表达式允许字母数字、下划线和点,但不允许连续的点:

I have a Regex to allow alphanumeric, underscore and dots but not consecutive dots:

^(?!.*?[.]{2})[a-zA-Z0-9_.]+$

我现在还需要允许在字符串的第一个和最后一个字符中使用点.

I also need to now allow dots in the first and last character of the string.

我该怎么做?

推荐答案

您可以像这样使用它并附加前瞻:

You can use it like this with additional lookaheads:

^(?!\.)(?!.*\.$)(?!.*?\.\.)[a-zA-Z0-9_.]+$

  • (?!\.) - 开始时不允许 .
  • (?!.*?\.\.) - 不允许有 2 个连续的点
  • (?!.*\.$) - 不允许 . 在末尾

    • (?!\.) - don't allow . at start
    • (?!.*?\.\.) - don't allow 2 consecutive dots
    • (?!.*\.$) - don't allow . at end
    • 这篇关于不允许连续点的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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