Skip to content

Instantly share code, notes, and snippets.

@scturtle
Last active May 2, 2020 04:33
Show Gist options
  • Save scturtle/8066503 to your computer and use it in GitHub Desktop.
Save scturtle/8066503 to your computer and use it in GitHub Desktop.
Answer to regex golf http://regex.alf.nu/
plain = r'foo'
anchor = r'k$'
ranges = r'^[a-f]*$'
backrefs = r'(...).*\1'
abba = (r'^(?!' # don't (
r'.*(.)(.)\2\1.*' # parttern like abba
r'$)') # ) select
plan = r'^(.)(.).*\2\1$'
prime = (r'^(?!' # don't (
r'(xx+?)\1+' # parttern that 'x' group (length > 1) is repeated
r'$)') # ) select
four = r'(.).\1.\1.\1'
order = r'^[a-h]*[i-z]*$' # cheat a bit, try to find a good split spot
triples = 'link: http://quaxio.com/triple/' # tough, see this blog that
# construct a FSM first
glob = (r'^(\w*) matches \1$|' # no star
r'^(\w*)\*(\w*) matches \2\w+\3$|' # one star
r'^(\w*)\*(\w*)\*(\w*) matches \4\w+\5\w+\6$|' # two star
r'^(\w*)\*(\w*)\*(\w*)\*(\w*) matches \7\w+\8\w+\9\w+\10$')
glob2 = (r'^(\*?)(\w*)(\*?)(\w*)(\*?)(\w*)'
r' .* ' # matches
r'((.(?!\1))+|\1)\2((.(?!\3))+|\3)\4((.(?!\5))+|\5)\6$')
# ((.(?!\1))+|\1) is used to conditionally match .+ if \1 if found (via @hadrel)
balance = r'^(<(<(<(<(<(<(<>)*>)*>)*>)*>)*>)*>)*$'
powers = (r'^(?!' # don't (
r'(x(xx)+)' # parttern that odd number of 'x'
r'\1*' # is repeated
r'$)') # ) select (via @plby)
@AppSynergy
Copy link

Nooo!
Wish I hadn't seen this. Must. not. cheat.

@teukon
Copy link

teukon commented Mar 1, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment