C
C#5mo ago
SwaggerLife

✅ Regex help pls:

^[a-z]+(?:_[a-z]+)?[a-z]+$ how can I check so that there are atleast 4 characters?
6 Replies
Thinker
Thinker5mo ago
At least 4 characters matching this pattern? {4,} iirc
SwaggerLife
SwaggerLife5mo ago
Exactly. Do I add it at the end?
Thinker
Thinker5mo ago
Well this pattern matches an entire line, so it'll only work if multi line is enabled But yes, add it after the pattern you want to match 4+ times eg. [a-z]{4,} is 4+ characters in the set a-z
SwaggerLife
SwaggerLife5mo ago
I don't get it. I want all the conditions to apply. But after that. I want to also add a condition that checks if the total is 4 characters at least. Does it make sense or is it just me? For instance I don't want to check whether the characters before _ are at least 4 or the otherway around. For instance something that would make this valid aa_a?
Thinker
Thinker5mo ago
I don't think you can easily check whether an the entire pattern matches at least 4 characters If you're running this from code (which I assume you are), you can always just check how long the resulting match is
SwaggerLife
SwaggerLife5mo ago
With the help of gpt4 I landed at this ^(?!_)(?!.*__)[a-z_]{4,15}(?<!_)$ which is working surprisingly 😂. Thanks for your help mate.