A regular expression does not start with a dot or dot with a dot

I need a regular expression that does not start with a dot or ends with [-_.]

Below the regular expression works, but is not executed for the first condition ie; does not start from a point.

^[A-Za-z0-9][^.]*[^-_.][A-Za-z0-9]$

for example: test.com must be a valid string, but it fails.

+4
source share
2 answers

From your previous question you can use:

^[^.].*[^-_.]$

But if you want to be able to match 1 character string, you will need negative images:

^(?![.])(?!.*[-_.]$).+

And if you want to match empty strings as well, just use *instead +.

^(?![.])(?!.*[-_.]$).*
+5

:

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

Source: https://habr.com/ru/post/1525272/


All Articles