Live data from Hacker News

Regex Golf

regex.alf.nu

81–90 of 189 posts

Re: Regex Golf

#81

Many times I would match the exact opposite opposite of what I wanted. Is there a general rule for inverting regexps? ^ and ?! don't seem general purpose.

Yeah, I couldn't figure out how to invert the abba one (.)(.)\2\1 without help.

Re: Regex Golf

#83

(.+|)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 was working on this pattern:

^(x{1,2}){1,2}$

But it wedged my browser as I started adding more terms. :/

Re: Regex Golf

#84

Earlier quoted context omitted.

well, if you are strictly interested in matching with the fewest characters, regardless of the apparent pattern: ^(.)(.).*\2\1$

Your anchors at the end make it match nothing.

sorry, misreplied. This was for palindromes.

Re: Regex Golf

#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.

Re: Regex Golf

#89

Many times I would match the exact opposite opposite of what I wanted. Is there a general rule for inverting regexps? ^ and ?! don't seem general purpose.

You need to pin the match to the start and end of the string with ^ and $ respectively otherwise the negation just matches an empty string or other irrelevant string.

Just to follow my thought pattern.

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.

Re: Regex Golf

#90

Does anybody know how to do math with regex? (triples) And conditionals don't seem to work?

From: http://quaxio.com/triple/

    ^([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])*$
Post reply on HN