Live data from Hacker News

Regex Golf

regex.alf.nu

91–100 of 189 posts

Re: Regex Golf

#91

Earlier quoted context omitted.

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, pu…

  ^(?!.*(.)(.)\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.

Re: Regex Golf

#92

Earlier quoted context omitted.

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, pu…

Remember

  (?!....) 
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)

Re: Regex Golf

#93
post #6

what's the pattern on "Abba"? I thought it was just to exclude doubled letters but I have doubles on two words on the left hand side as well (noisefully and effusive, in case the word lists are the same)

5. Abba (^unv|.u|z|ph|mi|st|vi|tan)

haha

Re: Regex Golf

#95
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*$

Or just (.).\1.\1.\1 for Four (198)

So long as we are going down this route, you can shave off another character with (.)(.\1){3} (199).

Re: Regex Golf

#97

Earlier quoted context omitted.

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, pu…

  >  ^(?!(.)(.)\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 :)

Re: Regex Golf

#99
post #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])*$

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