I have always struggled with these traits. I remember how the lecturer told us about everything, that if you have a problem requiring the use of regular expressions to solve it, you actually have 2 problems.
Well, of course, I agree with that. Regex is something that we donβt use very often, but when we do it, like reading some foreign language (well, for me itβs all the same) ... I think Iβll let me get the book and read on.
I have a problem: I need to check the username based on the following criteria:
- may contain letters, upper and lower
- may contain numbers
- may contain periods (
. ) and underscores ( _ ) - periods and underscores cannot be consecutive, i.e.
__ .. not allowed, but ._._ will be valid. - no more than 20 characters
So far I have had the following: ^[a-zA-Z_.]{0,20}$ , but of course this allows you to repeat underscores and periods.
Now I'm probably starting all this wrong with a set of valid characters and maximum length. I tried (unsuccessfully) to create some kind of look or look or something else to look for invalid period repetitions ( . ) And underscores ( _ ), not sure what approach or methodology to eliminate this requirement in regex.
Can someone help with a recommendation / alternative approach or point me in the right direction?
source share