Live data from Hacker News

Regex Golf

regex.alf.nu

161–170 of 189 posts

Re: Regex Golf

#161
post #14

Shouldn't the objective be to get the lowest score if it's called "golf?"

The golf part is that you can only get the most points by writing the shortest regex possible. However, I think the game avoids just giving a score based on the number of characters. To do that, you would be required to match all on the left and none on the right (the equivalent of actually getting the golf ball in the hole). This game lets you pick up your ball whenever you want and scores you based on distance to the hole. For the golfers, think of it more like a closest-to-the-hole competition than the more common stroke play.

Re: Regex Golf

#162
post #48
post #33

Earlier quoted context omitted.

Wait, so what's with the 'point system' here? Why's shdon answer for backrefs 199 and yours 201? (and while you're at it, could you briefly explain (...).*\1 for the regex newbies?)

Regex explained: (...) # Match exactly 3 (the dots) characters and save them as a group (the parenthesis) .* # Match any character (the dot) 0 or more times (the asterisk) \1 # Reuse the first group

backreferencing is new to me, but why this: (...).*(...)

isn't working !! ain't I back referencing the first group which is (...) ?

Re: Regex Golf

#164
post #146
post #87

Earlier quoted context omitted.

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 mult…

Thanks for the web site links! Both are pretty interesting and I actually learned something from the detailed description(s) that regex101 provides.

(I learned that for (\1)+, "Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data")

Re: Regex Golf

#166
post #148
post #128

Earlier quoted context omitted.

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

One more smidgen, behold 584:

    5[54]|2[437]|00($|[369]|1[25])|^[83][1729]|9.7

Re: Regex Golf

#167
post #146

Earlier quoted context omitted.

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 mult…

Thanks for the web site links! Both are pretty interesting and I actually learned something from the detailed description(s) that regex101 provides. (I learned that for (\1)+, "Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data")

Later, I realized that in a regular program / on the command line, the negative lookahead can be avoided by using the !~ (doesn't match) operator,i.e., we just check that the number is not a non-prime using a simpler regex:

  DB print "Matches!" if (("x" x 31) !~ /^(..+)\1+$/)
Matches!

  DB print "Matches!" if (("x" x 18) !~ /^(..+)\1+$/)
The Regex Golf site only asserts matches, i.e., it's using =~. That's why the negation using negative lookahead was needed.

(The simpler regex merely looks for non-primes by matching any number of characters which are a multiple of two numbers, n x m, i.e., those which can be factorized. n comes from (..+), m comes from \1+).

Re: Regex Golf

#168
Hrm, on number 8 "Four" using:

    (.)(.*\1){3,}
I got all but the "do not match" for "Ternstroemiaceae"

The challenge appeared to be to match words with four instances of the same letter. "Ternstroemiaceae" contains four 'e's, and thus should be in the "match" column, instead of the "don't match" column, no? Did I miss something?

Re: Regex Golf

#169

Hrm, on number 8 "Four" using: (.)(.*\1){3,} I got all but the "do not match" for "Ternstroemiaceae" The challenge appeared to be to match words with four instances of the same letter. "Ternstroemiaceae" contains four 'e's, and thus should be in the "match" column, instead of the "don't match" column, no? Did I miss something?

Look closer, there's something different about the matches and this word.

Re: Regex Golf

#170
post #148

Earlier quoted context omitted.

Nice. A smidge better at 582: 5[54]|2[437]|00($|[369]|1[25])|^8[17]|^3[29]|9.7

One more smidgen, behold 584: 5[54]|2[437]|00($|[369]|1[25])|^[83][1729]|9.7

586:

    ^[378][12479]|00($|[369]|1[25])|5[45]|2[347]
Post reply on HN