Regex Golf
regex.alf.nu
regex.alf.nu
^(?!(..+)(\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.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.
- 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.
(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")
DB<63> print "Matches!" if (("x" x 31) !~ /^(..+)\1+$/)
Matches! DB<64> 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+).
The scoring system for this is incremental which is the opposite of golf.
A proper scoring system with this would provide a character limit (par) for each section and the goal would be to write a shorter regex formula to complete the task. Final score would be how many characters under or over the total character limits (course par) you scored.
Seems this is more like Regex Darts or something like that.
But it's fun nonetheless.
The title was (most probably) derived from Code Golf, which is a competition in coding something with as few characers as possible. Code Golf was derived from Golf, where you want to use as few turns as possible.
The score going up and not down, which is done because you get more points the more objectives you fulfill, does not change the objective of this game or what it is based on.
Golf scoring penalizes you with more "points" by how many strokes you take.
If you are playing a Par 4 and it takes you 6 swings to get in the cup you just got _penalized_ +2
If your partner gets in the cup in 2 swings he is awarded -2
Therefor "Regex Golf" has its scoring reversed
This game is the opposite, meaning the higher the score the better.
This has nothing to do with the objectives of the games in question, just the scoring method.
To make the sport of golf have a similar scoring system as this game you would grant ten points for every stroke under par, deduct ten points for every stroke over par, and deduct one point for every penalty stroke.
Regex golf is to code golf as paintball golf is to normal golf. Or something.
I start with
(.)(.)\2\1
Now to invert I change it to
(?!(.)(.)\3\2)
As you say, the negation matches an empty string. So I put on anchors:
^(?!(.)(.)\3\2)$
But now it matches nothing. I'm not even sure what that regexp says. The entire string is a negation? Would anything match that regexp?
I see elsewhere on this page that the answer involves putting in an extra dummy character, putting that new negation-and-dummy-character in parens, and then requiring that, between the anchors, there be 0-or-more of negation-and-dummy-character.
^((?!(.)(.)\3\2).)∗$
Two questions:
1. Why are my backrefs still \3 and \2? I added another pair of parens. (I thought ?! might not count, but it counted in my second example above.
2. Why does abba no longer match? It has no matches to the negation-and-dummy character construct, which ∗ should match, right?
NB: I used ∗ as my asterisk to avoid bb-code.
^(?!.*(.)(.)\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.
I went with:
^(?!.(.)(.)\2\1).$
with the thought that if I really wanted those matches I would want the whole strings.
Fun game!
^(?!.*(.)(.)\2\1).*$
With proper formatting hopefully. (?!....)
is a lookaround, so it matches 0 characters. So ^(?!....)$
will only match an empty string! (Think about it). Try ^(?!....$)
instead (the $ anchor inside the lookaround) > ^(?!(.)(.)\3\2)$
> But now it matches nothing.
Things like (?!..) are called assertions. I like to think of assertions as patterns that match the space between two characters. So (?!a) means that it matches the space, where the next character is not 'a'.So in this case, your regex matches an empty string (since it doesn't match any character at all between ^ and $, only spaces.)
> ^((?!(.)(.)\3\2).)∗$
> 1. Why are my backrefs still \3 and \2?
Well, you're referencing the second and third opening parenthesis. > but it counted in my second example above
Maybe that's because your second example is wrong? I mean, I don't really know what you are trying to achieve in your second example.By the way, my solution for abba is
^(?!.*(.)(.)\2\1)
I guess you'll will be able to figure out what this regex means by now :) abac|accede|adead|babe|bead|bebed|bedad|bedded|bedead|bedeaf|caba|caffa|dace|dade|daff|dead|deed|deface|faded|faff|feed
Edit: /s ^x{2,3}$|^x{5}$|^x{7}$|^x{11}$|^x{13}$|^x{17}$|^x{19}$|^x{23}$|^x{29}$|^x{31}$|x{33}^(?!(..+)(\1)+$)
^[a-f]*$
Is the same length too.^(\⁕?)(\w⁕)(\⁕?)(\w⁕)(\⁕?)(\w⁕) .⁕ ((.(?!\1))+|\1)\2((.(?!\3))+|\3)\4((.(?!\5))+|\5)\6$
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.
Edit 2: Formatting to avoid the *s becoming italics :/
le[^*]|co|ito|dr|^p|su|gi|nr|hw|fa|[eo]b|ide
Glob 376 although it isn't pretty and better may be possible.Indent two spaces with a blank line above to avoid code mangling.
^([wlpb]|c[hor]|do|re|mi|\*[pifvt]|\*er)Glob 386 although not the cutest way to do it.
http://cstheory.stackexchange.com/questions/1854/is-finding-...
(.)(.*\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?
Only used 1 special char between them (anchoring something). So the first two sets have repeated 3 character sequences, and in one of them they follow an additional pattern.
For the third sequence, 8 character solution, lots of specials.
edit: Not in Glob. What is Glob about?
https://en.wikipedia.org/wiki/Globbing
I haven't figured out how to solve this with the parts of ERE and PCRE that I know. (I definitely don't know the entirety of PCRE.)
It's straightforward for me to write a substitution using regular expressions to create a pattern-matcher for a given glob (just anchor the ends and replace literal ? with . and literal * with .*) but here we have to do it inside a single regular expression.
I don't think there's a BRE solution if the number of stars is unbounded because I don't think this is a regular language.
If you look at the revisions, you'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.
5. 193 ^(?!.*(.)(.)\2\1)
11. 379 ^\*(er|[fiptv])|^([blpw]|c[hor]|do|mi|re)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.
And conditionals don't seem to work?
^([0369]|[258][0369]*[147]|[147]([0369]|[147][0369]*[258])*[258]|[258][0369]*[258]([0369]|[147][0369]*[258])*[258]|[147]([0369]|[147][0369]*[258])*[147][0369]*[147]|[258][0369]*[258]([0369]|[147][0369]*[258])*[147][0369]*[147])*$580pts: 00(0$|3|6|9|12|15)|[^0]14|.53|^3[^38]|55|43|23|9.7
5[54]|2[437]|00($|[369]|1[25])|^8[17]|^3[29]|9.7 5[54]|2[437]|00($|[369]|1[25])|^[83][1729]|9.7 ^[378][12479]|00($|[369]|1[25])|5[45]|2[347]Remember division rules? If the sum of digits is divisible by 3, then the number is divisible by 3
So, if you have 0, 3, 6, 9, it's as you can remove them, the value won't change
Remaining are the 2, 3, etc digit numbers divisible by 3
[369](?![7238])|(?:0)12
It's sloppy and certainly not correct, but a decent score. (.).*\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*$ (.)(.\1){3}
Order: 168 ^a*b*c*d*e*f*g*h*i*l*m*n*o*p*r*s*t*w*y*z*$
(You just didn't remove unused letters.)(.)...\1.\1
^[^o].....?$
Probably not what was wanted, but it works (or maybe it was to trick people onto a false path) ^[a-g][^airn]|[fs].*y$|ot$I must say Regex golf is a lot of fun.
^.{5}[^e]?$198 points
^([ab][c-gio][^d]|c[ehlo]|d|f[il]|gh|mo)
Not as clever as you guys though! ^<.*>$|^<<.*>>$|^<<<.*>>>$ ^(<(<(<(<(<(<<>>)*>)*>)*>)*>)*>)*$
If we had named captures: ^(((?<P><)|(?<-P>>))*(?(P)(?!)))$[<>]{28}
^[a-g]*[^a-g]*$ ^[a-i]*[l-z]*$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...
^(\*(er|[fiptv])|b|c(?!a)|do|le|mi|p|re|w) ^\*(er|[fiptv])|^([blpw]|c[hor]|do|re|mi)
and it has "do re mi" in it! ^.[^bds].*[^e-kjotz] .* [^eiz].+[^lx]..$ ^[lwp]|fa|r[ro]|[isytd]l|c$|de de|eat|fa|rr|ow|[rl]o|^p|[cd]$ You're allowed to cheat a little, since this one is technically impossible.
No idea how you cheat...EDIT, SPOILERS: I get 170 with ^(.?)(.)(.).?\3\2\1$
^(.)[^p].*\1$ # 177, "cheat a little"That said, 'regular expressions', as used in most programming languages, have extensions that extend the expressive power. One such extension is the matched group & backreference, used in other commentor's answers.
From a theoretic stance, these aren't really 'regular expressions', but that's what we call them in practice.
This confused me for a few minutes.
^((?!([aeiou])([^aeiou])\3\2).)*$
The following also works for the testcases and is shorter: ^((?!(.)(.)\3\2).)*$ ef|^((?!(.)\2).)*$
But I reckon yours wins for having a pair of breasts in the middle ^(.)(.).*\2\1$haha
Edit: ah, every word in left column has 4 a-f letters. So [a-f]{4} is a shorter match.
Kudos to the author of the game, good job.
Lol so awesome this. I oblige. Much love!
^((((((((((x)\10?)\9?)\8?)\7?)\6?)\5?)\4?)\3?)\2?)\1?$
I feel like there's gotta be a sneakier way of doing this. ^x{32}$|^(x{2}){1,8}$|^(x{64})+$|^x$ ^(x|(xx){1,9}|x{32}|(x{64})+)$^(x{1,2}){1,2}$
But it wedged my browser as I started adding more terms. :/
^x$|^(x+)\1$ ^(x|xx|xxxx|x{8}|x{16}|x{32}|(x{64})+)$^(((((((((xx?)\9?)\8?)\7?)\6?)\5?)\4?)\3?)\2?)\1?$
(60 points)
^((xx?|xxxx)(\2{7})?)(\1{63})?$