Reputation: 23
Hey so I'm looking for a regrex pattern that:
_
in a row but no more, can end start and have the _
in betweenSome examples of passes and fails are:
.test #fail
_test #pass
_.test #pass
__test #pass
test_ #pass
te_.st #pass
te.st #pass
..test #fail
So far I have ^[\w](?!.*?\.{2})(?!.*?_{2})[\w.]{1,28}$
, which works for everything but detecting the __
between and at the end of the words. Any help would be appreciated! This has been hurting my brain for hours
Upvotes: 2
Views: 70
Reputation: 370699
Allows for only one period in a row and can't start or end in a period
Negative lookahead for ^\.
, \.$
, and \.\.
from the beginning of the string
Allows for only two or one _ in a row but no more, can end start and have the _ in between
Negative lookahead for __
from the beginning of the string
Max 20, min 3 characters
After the other logic is implemented, match and consume 3-20 of the allowed characters
^(?!.*___|\.|.*\.$|.*\.\.)[a-z1-9_.]{3,20}$
https://regex101.com/r/TUjY13/2
^
- From start of string:(?!.*___|\.|.*\.$|.*\.\.)
- Make sure match doesn't:
.*___
- contain triple underscores\.
- start with a .
.*\.$
- end with a .
.*\.\.
- contain double periods[a-z1-9_.]{3,20}
- Match 3-20 characters in the string body using all allowed characters (case-insensitive)Upvotes: 3