Help with a regex
im not good with regex, tried GPT but his brain is not braining. How can i do a regex for:
- minimum 1 letter to maximum 10 letters in any position
- minimum 1 digits to maximum 10 digits in any position
- can include - and _ but will never start or end with those symbols and it will never count in the 10+10 characters count
So this would be valid:
ABCD1234
A1B2C3D4
A_1_b23-Fca
ABCDEFGHIJ1234567890
AB_CDE_FGHIJ-123456_789_0
A1B2C3D4E5F6G7H8I9J0
A1_B2C_3D4E5-F6G7H8I_9J0
i think you get it
18 Replies
So at most 10 characters can appear consecutively, and same goes for digits? And - and _ don't count when considering the 10-char/digit max substring rule? So 123456-78901 is not allowed? Also, I'm assuming that upper and lowercase characters are in the same character class and not considered separate?
numbers and letters can take any position as long as theres at least 1 letter and 1 number and maximum 10 of each. so 123456-78901 is not allowed but 123456-78901a would be. Also yes a-zA-Z
Sounds difficult to solve with regex. You'd need to express that "if there is already a letter, there must now also be a digit", but also "if there is a digit, there must now be a letter"
So that 1a and a1 are valid
But not 11 and aa
So I have no clue how you'd do that with regex
yeah, sounded really bad when GPT couldnt do it, doesnt look easy :S
how is 123456-78901 not allowed by `123456-78901a is? My point with 123456-78901 was that that's either a string of 11 digits or two strings of 6 and 5 digits, respectively, depending on whether the dash breaks up the group
There must be a minimum of 1 letter
I get that. I was just asking if - and _ are considered to "break up" a contiguous string and restart the count
- and _ will never count in the character count
oh ok, didnt understand you, no it should not break it, so in the full string it should only be up to 10 digits and up to 10 letters regardless of the - or _
i think its hard, maybe i should rethink of a way to integrate it with code instead of regex
okay so 123456-78901a is not allowed then, but 123456-7890a would be, yes
yes
also a123456-7890 can be allowed since it starts with letter
I think the rule of there needing to be at least 1 char and at least 1 digit, you could separate that from the rest. Cuz the rest is easy with a regex
also any combo with more added letters should work up to 10 letters
Yeah I mean you can possibly solve it with 2 regexes
i see, didnt think about it, will try to do it
okay, thanks you saved my day
will try this approach, tysm
Basically, the digits group could look like this:
(?:(?:_*-*[0-9]_*-*){1,10})
and the characters group could look like this: (?:(?:_*-*[a-zA-Z]_*-*){1,10})
and then the bulk of the regex would be (?:(?:(?:_*-*[a-zA-Z]_*-*){1,10})|(?:(?:_*-*[0-9]_*-*){1,10})*)
and then you'd have to handle the starting character
oh that would also need to check that no two digits/char groups occur back to back
or else the {1,10} is meaningless if you can just stick two together and make a 20If you can choose the method to undertake this; Don't use regex
Just write a method that iterates each character and keep track of what you have seen using variables
So from your message, have a counter for letters and digits. Start the loop, ensure it doesn't starts with a letter or number, then continue for each character and skip - and _. If at any point you find a character not in the expected range, it fails.
Writing a simple loop like this takes 5 minutes. Writing a complex regex that might solve all your use cases takes all morning and additional bugfixing sessions 😛
This cannot be correctly represented with regex.
Just for loop through that, check start and end for forbidden characters and have a counter for letters and numbers.