Regex that does not allow consecutive dots
You can use it like this with additional lookaheads:
^(?!\.)(?!.*\.$)(?!.*?\.\.)[a-zA-Z0-9_.]+$
-
(?!\.)
- don't allow.
at start -
(?!.*?\.\.)
- don't allow 2 consecutive dots -
(?!.*\.$)
- don't allow.
at end
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