I need to compose a regular expression for string, with a max length of 6 characters, containing only Latin letters in lowercase, with an optional underscore separator, without underscore starting and trailing.
I tried the following
^[a-z_]{1,6}$
But it allows underscore at the start and the end.
I also tried:
^([a-z]_?[a-z]){1,6}$
^(([a-z]+)_?([a-z]+)){1,6}$
^([a-z](?:_?)[a-z]){1,6}$
But nothing works. Please help.
Expecting:
Valid:
ex_bar
Not valid:
_exbar
exbar_
_test_
3
Answers
I would express your requirement as:
This pattern matches:
^
from the start of the string(?!.{7,}$)
assert that at most 6 characters are present[a-z]
first letter must bea-z
(?:[a-z_]*[a-z])*
matcha-z
or underscore in the middle, but onlya-z
at the end$
end of the stringNote that the behavior of the above pattern is that one character matches must be only letter
a-z
. Similarly, two character matches can also only bea-z
twice. With three character matches and longer, it is possible for underscore to appear in the middle.Here is a running demo.
This is a fairly simple pattern that should work
^(?!_)[a-z_]{0,5}[a-z]$
. See here for a breakdown.(?!^_)([a-z_]{6})(?<!_$)
You could use a negative look-ahead and negative look-behind to ensure that the string doesn’t start and end with an
_
underscore.https://regex101.com/r/sMho0c/1