Regex any number of _ between letters

Viewed 160

I'm trying to create a regex that allows to have any number of _ between letters and was to have between 5 to 10 letters.

Valid values:

a_b_c_d_e

a_b_c_________________d_e

abcde

aa_bcd

Non Valid values:

_abcde

adebc_

a_________________________b

So far I came up with this but doesn't allow any number of _

^[a-zA-Z][a-zA-Z_]{3,8}[a-zA-Z]$
1 Answers

Use

^[a-zA-Z](?:_*[a-zA-Z]){4,9}$

See proof

NODE EXPLANATION
^ the beginning of the string
[a-zA-Z] any character of: 'a' to 'z', 'A' to 'Z'
(?: group, but do not capture (between 4 and 9 times (matching the most amount possible)):
_* '_' (0 or more times (matching the most amount possible))
[a-zA-Z] any character of: 'a' to 'z', 'A' to 'Z'
){4,9} end of grouping
$ before an optional \n, and the end of the string
Related