Skip to content

Instantly share code, notes, and snippets.

@jonathanmorley
Forked from jpsim/answers.md
Last active November 16, 2022 17:42
Show Gist options
  • Star 64 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save jonathanmorley/8058871 to your computer and use it in GitHub Desktop.
Save jonathanmorley/8058871 to your computer and use it in GitHub Desktop.
Best possible answers collected so far for [Regex golf](http://regex.alf.nu/). === WARNING: SPOILERS ===

See Also:

Normal

Level Score Regex Credit
Warmup 207 foo
Anchors 208 k$
Ranges 202 ^[a-f]*$
Backrefs 201 (...).*\1 gorhill (HN)
Abba 195 ^(?!.*(.)\1)|ef BeniaminK (GH)
A man, a plan 177 ^(.)[^p].*\1$ hyp0 (HN)
Prime 286 ^(?!(..+)\1+$) josephlord (HN)
Four 199 (.)(.\1){3} chrismorgan (HN) / MereInterest (HN)
Order 199 ^.{5}[^e]?$ ekke (HN)
Triples 596 00($|3|6|9|12|15)|4.2|.1.+4|55|.17 alexandrosm (GH)
Glob 397 ai|c$|^p|[bcnrw][bnopr] nwellnhof (HN)
Balance 454 .{37}|^(<(..(?!<.>$))*>)*$ Davidebyzero (GH)
Powers 93 ^(?!(.(..)+)\1*$) plby (GH)
Long count 256 ((.+)0\2+1){8} Davidebyzero (GH)
Alphabetical 317 .r.{32}r|a.{10}te|n.n.. alexandrosm (GH)
Powers 2 88 ^(?!((xxx)+x|xx|)\1*$) muxrwc (GH)
Total 4075
Subtraction 180 ^(.+)(.+) - \1 = \2$ jonathanmorley (GH)
Typist 181 ^[adresbtcfxzgvw]+$ jonathanmorley (GH)

Hard Mode

Level Score Regex Credit
Warmup 207 foo
Anchors 206 ick$ Davidebyzero (GH)
Ranges 202 ^[a-f]*$
Backrefs 201 (...).*\1 gorhill (HN)
Abba 193 ^(?!.*(.)(.)\2\1) chingjun (HN) / josephlord (HN)
A man, a plan 150 ^(.?)(.?)(.?)(.?)(.?)(.?).?\6\5\4\3\2\1$ jonathanmorley (GH)
Prime 284 ^(?!(xx+)\1+$)xx Davidebyzero (GH)
Four 199 (.)(.\1){3} chrismorgan (HN) / MereInterest (HN)
Order 156 ^a*b*c*d*e*f*g*h*i*j*k*l*m*n*o*p*q*r*s*t*u*v*w*x*y*z*$ Davidebyzero (GH)
Triples 524 (?=((.*?[147]){3})*((.*?[147]|){2}))(?=((.*?[258]){3})*((.*?[258]|){2}))^.*$(\3\7|(?!\3|\7)\4\8|(?!\4|\8)) teukon (GH)
Glob 297 ai|c$|^p|[bcnrw][bnopr] nwellnhof (HN)
Balance 443 ^(<(<(<(<(<(<(<>)*>)*>)*>)*>)*>)*>)*$ gkucmierz (GH)
Powers 93 ^(?!(.(..)+)\1*$) plby (GH)
Long count 239 ^((?=(\S*)0).{4} (?=\2[1]))+1+$ Davidebyzero (GH)
Alphabetical 282 ^(?!.*\b(.*)(e|(n|r|(s|t))).* \1(a|(?!\3)[en]|(?!\4)[rs])) teukon (GH)
Powers 2 -12 ^(?!((xxx)+x|xx|)\1*$) muxrwc (GH)
Total 3664
Subtraction 180 ^(.+)(.+) - \1 = \2$ jonathanmorley (GH)
Typist 180 ^[adresbtcfxzgvwq]+$ jonathanmorley (GH)
@BeniaminK
Copy link

Abba: ^(?!.*(.)\1)|ef gives 195

@muxrwc
Copy link

muxrwc commented Sep 18, 2014

Power 2 : 88

^(?!((xxx)+x|xx|)\1*$)

@TedYBear
Copy link

would be interesting to have the best scores for hard mode too

@mattchainsaw
Copy link

Balance 336 points
^<.*>$|^$
Matches add but also matches some of the others sides.

@gkucmierz
Copy link

Is Balance input updated?
^(<(<(<(<(<(<(<>)>)>)>)>)>)>)*$
443 points

@cartmanez
Copy link

Powers 2(hard mode): ^(?!((xxx)+(x|xx)|xx|)\1*$)
83 points

@hyeonjae
Copy link

Typist 189 points
^[^h-puy]+$

@altoo
Copy link

altoo commented Mar 19, 2015

Powers 2(hard mod): ^(?!((xxx)+xx?|xx|)\1*$)
86 points

@hyeonjae
Copy link

Addition 165 points
^(x_)(x_)...(x_)(x_)...\1\3...\2\4$

@Dinoguy1000
Copy link

I can't figure out how you're supposed to cancel formatting inline; any pointers?

Glob (336 - hard)

^(.*)(\*?)(.*)(\*?)(.*)(\*?)(.*) .* \1((?!\2).+|\2)\3((?!\4).+|\4)\5((?!\6).+|\6)\7$

Powers 2 (86 - hard)

^(?!((...)+..?|..|)\1*$)

It seems like

^(?!((...)*..?|)\1*$)

should work, but it doesn't match anything and I can't figure out why.

Note: the following are obsoleted by https://gist.github.com/Davidebyzero/9090628, so I won't bother reporting any more solutions, but I'll leave these here anyways since they represent solutions I developed myself or tweaks/optimizations I made to others' solutions.

Subtraction (182 - both)

^(.*)(.*)- \2= \1$

Addition (167 - both)

^(.*)(.+) .(.+)(.*) =\3\1 . \2\4$

or

^(.+)(.*) .(.*)(.+) =\3\1 . \2\4$

or

^(.+)(.*) .(.+)(.*) =\3\1 . \2\4$

or

^(.+)(.*) .(.+)(.+) =\3\1 . \2\4$

Or if you're boring, you can just do this 👅

^(.*)(.*) .(.*)(.*) =\3\1 . \2\4$

Anyway (168 - normal; 68 - hard)

^[aeiouy]?(.[aeiouy])+[^aeiou]?$

This works for normal mode, but fails the extended cases for hard. I feel like I'm missing something obvious here.

Tic-tac-toe (142 - normal; 42 - hard)

(([OX])\2\2|([OX])...\3...\3|([OX])....\4....\4)

This doesn't match "OXX OX. XO.", but that doesn't look like a won/winnable tic-tac-toe game.

Edit: My current totals, after reading through a lot of comments, are: default levels: 4075 (normal), 3801 (hard); bonus levels: 1736 (normal), 1731 (hard). As far as I can tell, these are the best possible scores given currently known regexes.

@madeinqc
Copy link

Abba's max is 196:
^(?!(.)+\1)|ef

@zogwarg
Copy link

zogwarg commented Jun 24, 2015

Anyway (191 - HARD) based on @Dinoguy1000:
^[aeiouy]?([^aeiou][aeiouy])*[^aeiou]?$

@micheljung
Copy link

Typist in normal (185): ^[a-grstvwxz]+$

@ngc696
Copy link

ngc696 commented Nov 30, 2015

Powers 2 (87 - hard):
^((?=(x_)\2\2$)\2\2)_x$

@zeshanlb
Copy link

zeshanlb commented Jan 8, 2016

Tic-tac-toe (N:153) : XXX|OOO|X...X...X|O...O...O|X..X..X|X....X....X
Tic-tac-toe (H:133) : XXX|OOO|X...X...X|O...O...O|X..X..X|O..O..O|X....X....X|O....O....O

@frankbryce
Copy link

Based on @zeshanlb's last answer
Tic-tac-toe (N:156) : XXX|OOO|([OX])...\1...\1|X..X..X|X....X....X
Tic-tac-toe (H:142) : XXX|OOO|([XO])...\1...\1|X..X..X|O..O..O|([XO])(....\2){2}

@thebigbadbobby
Copy link

Powers 2 (86) ^(?!((xxx)+xx?|xx)\1*$). Hard mode.

@CatsAreFluffy
Copy link

Correct Powers 2: ^(?!(|xx|xx?(xxx)+)\1*$) 86
Tic-tac-toe: ([OX])\1\1|([OX])..\2..\2|([OX])...\3...\3|([OX])....\4....\4 139 Hard mode
Addition: ^(.*)(.*).(.*)(.*) .\3\2. \1\4$ Both 169

@JacobMathBoy
Copy link

Excuse me, but whoever got 180 points on Typist, can't I just use a few ranges to improve that? ^[a-gq-tvwxz]+$
(sings while looking at keyboard) A, B, C, D, E, F, G. H, I, J, K, LMNOP. Q, R, S. T, U, V. W, X, Y, Z.
Sorry. I just had to tell you that you could get five more points. 😄 👍

EDIT: You had 180, not 181. Sorry.

@The-Snide-Sniper
Copy link

The-Snide-Sniper commented May 8, 2016

Glob - Hard (341): ^(.*)(?=.*( .* \1))(\2|\*(.*)(\2.+\4|\*(.*)(\2.+\4.+\6|\*(.*)\2.+\4.+\6.+\8)))$
Long Count - Hard (245): ^((\d*)01* (?=\2(1)))*1*$
Powers 2 - Hard (76): x{16385}|^(?!(xx(xx)?(xxx)*)\1*$)x
Subtraction - Normal, Subtraction - Hard (182): ^(.+)(.+)- \2= \1
Typist - Normal, Typist - Hard (189): ^[^h-puy]*$

@micsthepick
Copy link

micsthepick commented Jun 7, 2016

No-one thought of these yet?
Tic-tac-toe: (Hard or robust) (168) (X|O)(\1|..(\1|.\1.|..\1..)..)\1
Powers 2: (Hard/robust) (89) ^((x+)\2(?=\2$)|x$)+$

@teukon
Copy link

teukon commented Sep 9, 2016

Addition (robust) (177): ^(.*)(.*)(.*) = \3\2\1$ (credit to Davidebyzero)
Tic-tac-toe (robust) (169): (\w)(\1|..(\1|.\1.|..\1..)..)\1 (nice work micsthepick!)

Each of the bonus problems can be done with fewer than 40 characters. Most can be done using no more than 30 characters.

@eromoe
Copy link

eromoe commented Apr 19, 2017

Any idea about this https://alf.nu/RegexGolf#accesstoken=Da3Q/rKFFqZwSic2o5Rt ?

It never ends – $ not allowed

Match all of these…
✔fu
✔tofu
✔snafu
and none of these…
✕futz
✕fusillade
✕functional
✕discombobulated

@altair21
Copy link

For It never ends - $ not allowed:
u\b

@tmzh
Copy link

tmzh commented May 19, 2017

A man, a plan: ^(.)(.).*\2\1$

@depperm
Copy link

depperm commented Jul 27, 2017

Abba: ^(?!(.)+\1)|.u

@Davidebyzero
Copy link

I've updated Best known Regex Golf solutions. The current Classic level set is fully covered. It's now in table format, including full credits and date-time stamps, a history of gradual improvements for most of the levels, and some extra bonus information.

I have intentionally not updated the Teukon level set solutions compilation, nor posted one for Holiday, as with the release of functioning high score lists in the newly brought back Regex Golf site, to do so would further taint the high scores with people who simply Google it and copy the solutions. This is not an ideal situation by any means, as I'd really like for everyone to be able to benefit from this knowledge if they so desire, but given the way the on-site high score lists currently function, I see no alternative. Having date-time stamps in the on-site high score lists would be a good start towards fixing this problem.

@837951602
Copy link

Subtraction ^(x+)(.+)- \2= \1$, no full space

@KorayUlusan
Copy link

New level: It never ends – $ not allowed
3 chars: u\b

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