Reputation: 33
As the title says, I need a regex to detect a combination of strings and numbers that is 10-20 in length and needs to have at least 3 capital letters and at least 2 lowercase letters, whilst ignoring any special characters.
What it should match
https://youtube.com/abcDeFF1234|
In this sequence, only abcDeFF1234 should be matched
sasddasxaDis3fsc|5MkCDXlmvWYLJGxD||sknsnsjsjs
In this sequence, only 5MkCDXlmvWYLJGxD should be matched
ABCab1asuxhausihx
In this sequence, everything should be matched
'Hey guys check out https://youtube.com/abcDeFFF1234 it's so cool!' And in this sequence, only abcDeFFF1234 should be matched
What it shouldn't match
https://gif.com//images/114fds2klcee7e7fb7a/tenor.gif?itemid=1389
DDD<73010825822784709>dd
abcA|CDA1123|
Additional info
And if this helps, the regex /(?=.*[A-Z].*[A-Z].*[A-Z])(?=\d*[A-Za-z])(?=[a-zA-Z]*\d)[A-Za-z0-9]{10,20}\b/
already satisfies most requirements but in the case of sasddasxaDis3fsc|5MkCDXlmvWYLJGxD||sknsnsjsjs
it matches with sasddasxaDis3fsc instead of 5MkCDXlmvWYLJGxD.
Upvotes: 1
Views: 57
Reputation: 424983
Try this:
\b(?=([a-z\d]*[A-Z]){3})(?=([A-Z\d]*[a-z]){2})[a-zA-Z\d]{10,20}\b
See live demo.
Your problem was that the dot in the look aheads should be a character class of the allowable chars, otherwise the look ahead looks beyond the end of the term being matched. Eg I changed:
(?=(.*[A-Z]){3})
to:
(?=([a-z\d]*[A-Z]){3})
Upvotes: 2