Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex string doesn't contain 2 dots in a row

Tags:

regex

I'd like to know if this regex expression is correct for checking that a string doesn't start with a dot, doesn't end with a dot and contains at least one dot anywhere but not the start or end:

My issue is that I can't figure on how to check if there's 2 dots in a row.

/^([^.])+([.])+.*([^.])$/

like image 779
tempestor Avatar asked Dec 15 '16 12:12

tempestor


2 Answers

It seems you need to use

^[^.]+(?:\.[^.]+)+$

See the regex demo

Details:

  • ^ - start of string
  • [^.]+ - 1+ chars other than a . (so, the first char cannot be .)
  • (?:\.[^.]+)+ - 1 or more (thus, the dot inside a string is obligatory to appear at least once) sequences of:
    • \. - a dot
    • [^.]+ - 1+ chars other than . (the + quantifier makes a char other than . appear at least once after a dot, thus, making it impossible to match the string with 2 dots on end)
  • $ - end of string.
like image 60
Wiktor Stribiżew Avatar answered Sep 21 '22 17:09

Wiktor Stribiżew


You're close, have a try with:

^[^.]+(?:\.[^.]+){2,}$

It maches strings that have 2 or more dot, but not at the begining or at the end.

If you want one or more dot:

^[^.]+(?:\.[^.]+)+$

If you want one or two dots:

^[^.]+(?:\.[^.]+){1,2}$
like image 37
Toto Avatar answered Sep 21 '22 17:09

Toto