Live data from Hacker News

Regex Golf

regex.alf.nu

181–189 of 189 posts

Re: Regex Golf

#185
What are the rules? That is, are these Perl regexes, POSIX regexes, …? (Come to that, what is this site? Going up one level to alf.nu gives me a lot of suggestions for what I can do by modifying the address, but no clue of who's doing it on my behalf.)

Re: Regex Golf

#186
post #172

Earlier quoted context omitted.

improved, gives 80 ^(x|(xx){1,9}|x{32}|(x{64})+)$

I tried to get rid of those redundant ^ and $ but it somehow didn't work. I probably forgot to put it all inside one group.

They are not redundant. Any string of one or more exes is going to constrain a substring of exes of length 2^n (consider n=0 for a trivial proof), so you do need those anchors!

Re: Regex Golf

#187
post #162
post #48

Earlier quoted context omitted.

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

It isn't working because (...) is just matching 3 characters. What your regex is saying, is basically: Match any 3 characters, then any character 0 or more times, then any 3 characters.

You're reusing the regex and not the match. Back referencing essentially means that you'll match whatever is matched the first time, rather than reusing the regex pattern.

I hope that made a little sense. Grouping, capturing, matching and backreferences is part of what makes regex tricky and very powerful. You might want to use some of the online tools, which can help explain the regex visually.

Re: Regex Golf

#188
post #186
post #172

Earlier quoted context omitted.

I tried to get rid of those redundant ^ and $ but it somehow didn't work. I probably forgot to put it all inside one group.

They are not redundant. Any string of one or more exes is going to constrain a substring of exes of length 2^n (consider n=0 for a trivial proof), so you do need those anchors!

I meant my previous post where I had them in every single possibility.

Re: Regex Golf

#189
post #187
post #162

Earlier quoted context omitted.

backreferencing is new to me, but why this: (...).*(...) isn't working !! ain't I back referencing the first group which is (...) ?

It isn't working because (...) is just matching 3 characters. What your regex is saying, is basically: Match any 3 characters, then any character 0 or more times, then any 3 characters. You're reusing the regex and not the match. Back referencing essentially means that you'll match whatever is matched the first time, rather than reusing the regex pattern. I hope that made a little sense. Grouping, capturing, matching…

thanks for clearing that for me, I figured it out my self after giving it sometime.
Post reply on HN