Live data from Hacker News

Regex Golf

regex.alf.nu

141–150 of 189 posts

Re: Regex Golf

#142

For Abba... Why doesn't (.)(.)\2[^\1] work? I thought backreferences matched the captured literal, so negating it would match? But this looks the same as (.)(.)\2\1...

You cannot negate a capture, only a character literal. A capture might only be a character but it is NOT a literal,

Gotcha. That makes perfect sense. Thank you! :)

Re: Regex Golf

#143
post #43
post #31

I got Four for 196 with (.).*\1.\1.*\1 and Order for 156 with ^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*$

Order for 198: ^[^o].....?$ Probably not what was wanted, but it works (or maybe it was to trick people onto a false path)

Nice, turn it around for 199:

  ^.{5}[^e]?$

Re: Regex Golf

#146
post #87

^(?!(..+)(\1)+$) 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.

Nice find. It works because it rejects "2 or more x's" repeated "2 or more times". So xx doesn't get rejected, but any multiple of that (xxxx, xxxxxx, ...) will be. The same way xxx doesn't get rejected, but any multiple of that (xxxxxx, xxxxxxxxx, ...) will be. You've solved it using the actual definition of prime numbers, no trickery needed. Well played. FYI, you don't need brackets around the \1, so can score 286.

More interesting than the definition of primes, it's almost the definition of multiplication that is embedded in this regex. We have two numbers(of occurrences) being multiplied:

- the first one is represented by the group (..+) it represents the number of occurrences n between 2 and +∞

- the second one is represented by (\1)+. We will repeat the first number m times, between 1 and +∞ times.

So the result of the multiplication is n*(m+1), which cannot be a prime. We just have to take the opposite with negative lookahead. It's very beautiful indeed.

See http://regex101.com/r/qN2fQ8 or http://www.regexper.com/#^%28%3F!%28..%2B%29\1%2B%24%29 to follow the above explanations.

Re: Regex Golf

#147

(.+|)foo(.+|) Lol so awesome this. I oblige. Much love!

Powers: ^((((((((((x)\10?)\9?)\8?)\7?)\6?)\5?)\4?)\3?)\2?)\1?$ I feel like there's gotta be a sneakier way of doing this.

I did similarly, but managed to squeeze out a few more points:

^(((((((((xx?)\9?)\8?)\7?)\6?)\5?)\4?)\3?)\2?)\1?$

(60 points)

Re: Regex Golf

#148
post #128

Earlier quoted context omitted.

569pts: ([^31]0|31|[017]2|[03]03|[^1]4|(900|01|7)5|6|[48]7|[57]8|09)$

Originally had 568, then saw this and improved. :) 580pts: 00(0$|3|6|9|12|15)|[^0]14|.53|^3[^38]|55|43|23|9.7

Nice. A smidge better at 582:

    5[54]|2[437]|00($|[369]|1[25])|^8[17]|^3[29]|9.7

Re: Regex Golf

#150

Earlier quoted context omitted.

^(?!.*(.)(.)\2\1) You may also need to fill in the places where it could be anything. The above worked on abba for me. BTW a double space indent then formats as code on HN I think.

Your approach scores higher, but it only matches the (imaginary) space before the "good" words. I went with: ^(?!. (.)(.)\2\1). $ with the thought that if I really wanted those matches I would want the whole strings. Fun game!

oops,

  ^(?!.*(.)(.)\2\1).*$
With proper formatting hopefully.
Post reply on HN