TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Regex Golf

285 pointsby subbzover 11 years ago

33 comments

josephlordover 11 years ago
<p><pre><code> ^(?!(..+)(\1)+$) </code></pre> Why does that work on primes? I got it by mistake when fiddling with the parenthesis locations but I was expecting to have to deal with xx separately.
评论 #6942186 未加载
评论 #6943051 未加载
chazover 11 years ago
Shouldn&#x27;t the objective be to get the lowest score if it&#x27;s called &quot;golf?&quot;
评论 #6941556 未加载
评论 #6945642 未加载
davidbrentover 11 years ago
I have the most trouble with regex, and something about seeing my matches as I type made this incredibly useful for me!
评论 #6941770 未加载
评论 #6941935 未加载
评论 #6941543 未加载
评论 #6941810 未加载
评论 #6941971 未加载
danielweberover 11 years ago
Many times I would match the exact opposite opposite of what I wanted. Is there a general rule for inverting regexps? ^ and ?! don&#x27;t seem general purpose.
评论 #6942104 未加载
评论 #6942128 未加载
elwellover 11 years ago
I figured out Ranges!<p><pre><code> abac|accede|adead|babe|bead|bebed|bedad|bedded|bedead|bedeaf|caba|caffa|dace|dade|daff|dead|deed|deface|faded|faff|feed </code></pre> Edit: &#x2F;s
评论 #6942588 未加载
评论 #6942592 未加载
hadrelover 11 years ago
Glob (333) without cheating (replace ⁕s with asterisks, they get turned into italics):<p>^(\⁕?)(\w⁕)(\⁕?)(\w⁕)(\⁕?)(\w⁕) .⁕ ((.(?!\1))+|\1)\2((.(?!\3))+|\3)\4((.(?!\5))+|\5)\6$<p>Edit: ((.(?!\1))+|\1) is used to conditionally match .+ iff a * has been found. .(?!\1) Matches any character if it is followed by \1. When * has been found then it matches no character, when * is not found it matches every character.<p>Edit 2: Formatting to avoid the *s becoming italics :&#x2F;
评论 #6943465 未加载
评论 #6944884 未加载
sbirchover 11 years ago
An interesting bit on the computational complexity of solving this problem (with a slightly different scoring function):<p><a href="http://cstheory.stackexchange.com/questions/1854/is-finding-the-minimum-regular-expression-an-np-complete-problem" rel="nofollow">http:&#x2F;&#x2F;cstheory.stackexchange.com&#x2F;questions&#x2F;1854&#x2F;is-finding-...</a>
ColinDabritzover 11 years ago
Hrm, on number 8 &quot;Four&quot; using:<p><pre><code> (.)(.*\1){3,} </code></pre> I got all but the &quot;do not match&quot; for &quot;Ternstroemiaceae&quot;<p>The challenge appeared to be to match words with four instances of the same letter. &quot;Ternstroemiaceae&quot; contains four &#x27;e&#x27;s, and thus should be in the &quot;match&quot; column, instead of the &quot;don&#x27;t match&quot; column, no? Did I miss something?
评论 #6946125 未加载
amixover 11 years ago
JavaScript solver: <a href="https://gist.github.com/amix/8063003" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;amix&#x2F;8063003</a> ;-)
daGrevisover 11 years ago
I like the idea, but words there seems to be pretty random. I can&#x27;t figure out the pattern, not even talking about writing regex... :(
评论 #6941386 未加载
评论 #6941842 未加载
jpsimover 11 years ago
Gist with my answers: <a href="https://gist.github.com/jpsim/8057500" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;jpsim&#x2F;8057500</a><p>If you look at the revisions, you&#x27;ll see my 1st iteration was mostly identifying patterns, then with more and more cheating (and looking at this thread) to squeeze every point possible.
评论 #6942934 未加载
评论 #6942439 未加载
JadeNBover 11 years ago
What are the rules? That is, are these Perl regexes, POSIX regexes, …? (Come to that, what <i>is</i> this site? Going up one level to alf.nu gives me a lot of suggestions for what I can do by modifying the address, but no clue of who&#x27;s doing it on my behalf.)
hyp0over 11 years ago
challenge: use machine learning to find the best solutions.<p>They might improve on those intended by exploiting accidental regularity in the corpus - though charmingly, the golf-cost of regex length helps combat this overfitting. They might also find genuinely cleverer solutions.
hademover 11 years ago
Reminds me of Vim Golf.<p><a href="http://vimgolf.com/" rel="nofollow">http:&#x2F;&#x2F;vimgolf.com&#x2F;</a>
The_Doubleover 11 years ago
Does anybody know how to do math with regex? (triples)<p>And conditionals don&#x27;t seem to work?
评论 #6942271 未加载
评论 #6942452 未加载
评论 #6942047 未加载
评论 #6942480 未加载
johnlbevan2over 11 years ago
Ternstroemiaceae contains four es; anyone else hit that issue &#x2F; know what that&#x27;s not in the valid results for Four? I&#x27;m guessing there&#x27;s a pun in there that I didn&#x27;t get :&#x2F;
lsv1over 11 years ago
Fun learning tool for regex.
评论 #6941598 未加载
Trufaover 11 years ago
I like the concept but the word choice doesn&#x27;t seem too &quot;regular&quot;, it is more about catching all the particulars rather than finding a pattern as far as I can tell.
评论 #6941374 未加载
cpetersoover 11 years ago
What purpose do the &quot;plain strings&quot; serve?
tareqakover 11 years ago
I got Four for 196 with<p><pre><code> (.).*\1.\1.*\1 </code></pre> and Order for 156 with<p><pre><code> ^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*$</code></pre>
评论 #6941723 未加载
评论 #6941827 未加载
评论 #6941664 未加载
评论 #6941923 未加载
评论 #6941786 未加载
评论 #6942944 未加载
评论 #6941828 未加载
评论 #6941668 未加载
scott_karanaover 11 years ago
For Abba...<p>Why doesn&#x27;t (.)(.)\2[^\1] work?<p>I thought backreferences matched the captured literal, so negating it would match? But this looks the same as (.)(.)\2\1...
评论 #6943835 未加载
评论 #6943821 未加载
onaclov2000over 11 years ago
Glob: 277, it didn&#x27;t match all and not match the others, but it&#x27;s reasonably high. ^([bcdlmpwr]|\*[efptv])
评论 #6942361 未加载
ZirconCodeover 11 years ago
For &quot;6. A man, a plan&quot;, I thought it was impossible to match palindromes with regex, am I wrong?
评论 #6941467 未加载
评论 #6945275 未加载
评论 #6941468 未加载
endophageover 11 years ago
Seems very very very broken. The regex &quot;[a]&quot; apparently matches &quot;crenel&quot;
评论 #6944013 未加载
评论 #6944068 未加载
Pxtlover 11 years ago
I hit enter and nothing happens.
评论 #6941754 未加载
bencoderover 11 years ago
what&#x27;s the pattern on &quot;Abba&quot;? I thought it was just to exclude doubled letters but I have doubles on two words on the left hand side as well (noisefully and effusive, in case the word lists are the same)
评论 #6941393 未加载
评论 #6941857 未加载
评论 #6942322 未加载
joelanmanover 11 years ago
am I being a bit slow? Why doesn&#x27;t [^g-z] work on &#x27;ranges&#x27;?
评论 #6941635 未加载
评论 #6942181 未加载
dsschnauover 11 years ago
Me and a coworker totaled 3079 points. Anyone beat it?
评论 #6944361 未加载
ddebernardyover 11 years ago
Doesn&#x27;t seem to do anything on an iPad...
xarienover 11 years ago
Why is Kesha an optimal answer? (#2 k$) ;)
jhightover 11 years ago
Spoiler alert (201 points): f[ao][no]
评论 #6942434 未加载
评论 #6942137 未加载
评论 #6942251 未加载
easy_riderover 11 years ago
(.+|)foo(.+|)<p>Lol so awesome this. I oblige. Much love!
评论 #6941761 未加载
sriharisover 11 years ago
^ answers all questions.