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.
Regex Golf
81–90 of 189 posts
Re: Regex Golf
#82Spoiler alert (201 points): f[ao][no]
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.
^(x{1,2}){1,2}$
But it wedged my browser as I started adding more terms. :/
Re: Regex Golf
#84Re: Regex Golf
#85am I being a bit slow? Why doesn't [^g-z] work on 'ranges'?
Re: Regex Golf
#86Re: 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.
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
#88Spoiler alert (201 points): f[ao][no]
Re: Regex Golf
#89Many 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.
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
#90Does anybody know how to do math with regex? (triples) 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])*$