Regex that does not allow consecutive dots

后端 未结 3 1447
我在风中等你
我在风中等你 2020-11-29 13:18

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

3条回答
  •  日久生厌
    2020-11-29 13:38

    Re-write the regex as

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

    or (in case your regex flavor is ECMAScript compliant where \w = [a-zA-Z0-9_]):

    ^\w+(?:\.\w+)*$
    

    See the regex demo

    Details:

    • ^ - start of string
    • [a-zA-Z0-9_]+ - 1 or more word chars
    • (?:\.[a-zA-Z0-9_]+)* - zero or more sequences of:
      • \. - a dot
      • [a-zA-Z0-9_]+ - 1 or more word chars
    • $ - end of string

提交回复
热议问题