Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression match for specific lower_case php validation [duplicate]

Tags:

regex

php

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_

like image 934
Vlad K Avatar asked Jul 03 '26 09:07

Vlad K


1 Answers

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

like image 50
JRiggles Avatar answered Jul 06 '26 02:07

JRiggles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!