puzzle, regex (screenreader-unfriendly) 

So, here's a regular expression (JS flavour):

^(?=( +|\t+))\1(?:\t| )

This regex matches (at the start of a line) both "1+ spaces followed by a tab" and "1+ tabs followed by a space", but it does *not* match a sequence of just spaces or just tabs.

How does it work?

(Make sure to CW your answers please!)

solution 

@joepie91 the first part matches as many as possible of whichever character it finds, so if the line were all of one character then it would match all of it and leave none for the second part.

re: solution 

@autumnull All of that is correct, but what role do the lookahead and backreference play here?

re: solution 

@joepie91 disables the backtracking down alternate paths and locks it into the “i’ve matched all the characters on this line” route

Follow

re: solution 

@autumnull Bingo, that's exactly it :)

· · Web · 1 · 0 · 1

re: solution 

@autumnull I actually found it in a real-world codebase that I was auditing! It was in ESLint, and I was staring at it for like 10 minutes before I finally realized how it was meant to work, and figured it'd be a neat puzzle to share :D

Sign in to participate in the conversation
Pixietown

Small server part of the pixie.town infrastructure. Registration is closed.