regex - how to limit, characters between a range using regular expression -


as far know {} curly braces used limit characters in regular expression {3,12}, match character length between 3 12.

i trying validate username might contain period . or _ either one, not both, doesn't matter placement. below regex working well.

(^[a-z0-9]+$)|(^[a-z0-9]*[\.\_][a-z0-9]*$) 

but need limit string length between 3 12, had tried put {3,12} in regex, doesn't work.

((^[a-z0-9]+$)|(^[a-z0-9]*[\.\_][a-z0-9]*$)){3,12} 

see example: https://regex101.com/r/kn3ao1/1

as hwnd suggested, simpler solution be:

^(?=.{3,12}$)[a-z0-9]+(?:[._][a-z0-9]+)?$ 

old solution, rather complex , convoluted,is left here reference, use 1 above instead.

^(?!(?:.{13,}|.{1,2})$)(?:([a-z0-9]+)|([a-z0-9]*[\.\_][a-z0-9]*))$ 

you can add lookahead this.

demo on regex101


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -