I'm trying to create 2 separate regexes for strings that have these two patterns
STTSHP.COM
- uppercase
- one letter repeats ONLY twice
- no digits
- only ever 6 letters before the .com
YNGFUV.COM
- uppercase
- no letter repeats
- no digits
- only ever 6 letters before the .com
I came up with this regex, but it doesn't seem to be working. What about it is wrong?
^(?=(?:[A-Z\D]*[A-Z]{6})(?:([A-Z\D])(?!.*\1)){6}\.COM$
CodePudding user response:
Based on my understanding of the problem here is a regex that would fulfil all conditions:
^([A-Z])(?![A-Z]*\1)(?:([A-Z])\2(?!\2)(?:([A-Z])(?![A-Z]*\3)){3}|(?:([A-Z])(?![A-Z]*\4)){5})\.COM$
RegEx Details:
^
: Start([A-Z])
: Match an uppercase letter and capture in group #1(?!\1)
: Make sure first letter is not repeated(?:
: Stare non-capture group([A-Z])\2
: Match an uppercase letter and capture in group #2. Match same letter again using\2
.(?!\2)
: Make sure same letter is not repeated again 3rd time(?:([A-Z])(?!\3)){3}
: Match any 3 letters ensure there are no adjacent repeats|
: OR(?:([A-Z])(?!\4)){5}
: Match 5 uppercase letter that don't allow any adjacent repeats
)
: End non-capture group\.COM
: Match.COM
$
: End
CodePudding user response:
You can assert that there are no 3 of the same uppercase chars.
^(?![A-Z]*([A-Z])[A-Z]*\1[A-Z]*\1)[A-Z]{6}\.COM$
The pattern in parts matches:
^
Start of string(?!
Negative lookahead, assert what to the right is not[A-Z]*([A-Z])[A-Z]*\1[A-Z]*\1
Match 3 of the same uppercase chars
)
Close the lookahead[A-Z]{6}
\.COM$
Match.COM
at the end of the string
See a regex 101 demo.