Introduction
I don't see many regex challenges on here, so I would like to offer up this deceptively simple one which can be done in a number of ways using a number of regex flavours. I hope it provides regex enthusiasts with a bit of fun golfing time.
Challenge
The challenge is to match what I've very loosely dubbed an "egalitarian" series: a series of equal numbers of different characters. This is best described with examples.
Match:
aaabbbccc
xyz
iillppddff
ggggggoooooollllllffffff
abc
banana
Don't match:
aabc
xxxyyzzz
iilllpppddff
ggggggoooooollllllfff
aaaaaabbbccc
aaabbbc
abbaa
aabbbc
To generalize, we want to match a subject of the form (c1)n(c2)n(c3)n...(ck)n
for any list of characters c1
to ck
, where ci != ci+1
for all i, k > 1, and n > 0.
Clarifications:
Input will not be empty.
A character may repeat itself later in the string (eg. "banana")
k > 1
, so there will always be at least 2 different characters in the string.You can assume only ASCII characters will be passed as input and no character will be a line terminator.
Rules
(Thank you to Martin Ender for this excellently-stated block of rules)
Your answer should consist of a single regex, without any additional code (except, optionally, a list of regex modifiers required to make your solution work). You must not use features of your language's regex flavour that allow you to invoke code in the hosting language (e.g. Perl's e
modifier).
You can use any regex flavour which existed before this challenge, but please specify the flavour.
Do not assume that the regex is anchored implicitly, e.g. if you're using Python, assume that your regex is used with re.search and not with re.match. Your regex must match the entire string for valid egalitarian strings and yield no matches for invalid strings. You may use as many capturing groups as you wish.
You may assume that the input will always be a string of two or more ASCII characters not containing any line-terminators.
This is regex golf, so the shortest regex in bytes wins. If your language requires delimiters (usually /.../
) to denote regular expressions, don't count the delimiters themselves. If your solution requires modifiers, add one byte per modifier.
Criteria
This is good ol' fashioned golf, so forget efficiency and just try to get your regex as small as possible.
Please mention which regex flavour you have used and, if possible, include a link showing an online demo of your expression in action.
banana
is egalitarian. \$\endgroup\$