I need a regular expression that does not start with a dot or end with [-_.]
.
This regex works but fails for the first condition; it does not start with dot:
^[A-Za-z0-9][^.]*[^-_.][A-Za-z0-9]$
For example: test.com
should be a valid string but it fails.
(dot) metacharacter, and can match any single character (letter, digit, whitespace, everything). You may notice that this actually overrides the matching of the period character, so in order to specifically match a period, you need to escape the dot by using a slash \.
The \b metacharacter matches at the beginning or end of a word.
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" .
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .
Using negative lookaheads to assert your requirements for the string:
^(?!^\.)(?!.*[-_.]$)[a-zA-Z0-9]+$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With