skip to Main Content

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


  1. I would express your requirement as:

    ^(?!.{7,}$)[a-z](?:[a-z_]*[a-z])*$
    

    This pattern matches:

    • ^ from the start of the string
    • (?!.{7,}$) assert that at most 6 characters are present
    • [a-z] first letter must be a-z
    • (?:[a-z_]*[a-z])* match a-z or underscore in the middle, but only a-z at the end
    • $ end of the string

    Note 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 be a-z twice. With three character matches and longer, it is possible for underscore to appear in the middle.

    Here is a running demo.

    Login or Signup to reply.
  2. This is a fairly simple pattern that should work ^(?!_)[a-z_]{0,5}[a-z]$. See here for a breakdown.

    Login or Signup to reply.
  3. (?!^_)([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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search