Reputation: 2246
I struggle with regular expressions, and I've been trying to make one that can match any 5-character combination of X
and O
, but ignore it if it repeats X
or O
EXACTLY 5 times.
This is what I came up with:
X{1,4}|O{1,4}
X|O{1,4}
those expressions match (I want it to ignore the XXXXX and OOOOO):
I also tried using the non-capturing group (?:)
, but it didn't work out too well.
Upvotes: 3
Views: 133
Reputation: 67968
^(?!(.)\1+$)[XO]{5}$
Try this.See demo.
https://regex101.com/r/uK9cD8/1
Upvotes: 5
Reputation: 174726
You may try the below assertion based regex.
^(?!(?:X{5}|O{5})$)(?=.*X)(?=.*O)[XO]{5}$
Upvotes: 2