this post was submitted on 31 Oct 2024
329 points (97.1% liked)

Programmer Humor

32380 readers
1310 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 

^.?$|^(..+?)\1+$

Matches strings of any character repeated a non-prime number of times

https://www.youtube.com/watch?v=5vbk0TwkokM

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 134 points 1 week ago* (last edited 6 days ago) (17 children)

The answer given in the spoiler tag is not quite correct!

Test caseAccording to the spoiler, this shouldn't match "abab", but it does.

Corrected regexThis will match what the spoiler says: ^.?$|^((.)\2+?)\1+$

Full workupAny Perl-compatible regex can be parsed into a syntax tree using the Common Lisp package CL-PPCRE. So if you already know Common Lisp, you don't need to learn regex syntax too!

So let's put the original regex into CL-PPCRE's parser. (Note, we have to add a backslash to escape the backslash in the string.) The parser will turn the regex notation into a nice pretty S-expression.

> (cl-ppcre:parse-string "^.?$|^(..+?)\\1+$")
(:ALTERNATION
 (:SEQUENCE :START-ANCHOR (:GREEDY-REPETITION 0 1 :EVERYTHING) :END-ANCHOR)
 (:SEQUENCE :START-ANCHOR
  (:REGISTER
   (:SEQUENCE :EVERYTHING (:NON-GREEDY-REPETITION 1 NIL :EVERYTHING)))
  (:GREEDY-REPETITION 1 NIL (:BACK-REFERENCE 1)) :END-ANCHOR))

At which point we can tell it's tricky because there's a capturing register using a non-greedy repetition. (That's the \1 and the +? in the original.)

The top level is an alternation (the | in the original) and the first branch is pretty simple: it's just zero or one of any character.

The second branch is the fun one. It's looking for two or more repetitions of the captured group, which is itself two or more characters. So, for instance, "aaaa", or "abcabc", or "abbaabba", but not "aaaaa" or "abba".

So strings that this matches will be of non-prime length: zero, one, or a multiple of two numbers 2 or greater.

But it is not true that it matches only "any character repeated a non-prime number of times" because it also matches composite-length sequences formed by repeating a string of different characters, like "abcabc".

If we actually want what the spoiler says — only non-prime repetitions of a single character — then we need to use a second capturing register inside the first. This gives us:

^.?$|^((.)\2+?)\1+$.

Specifically, this replaces (..+?) with ((.)\2+?). The \2 matches the character captured by (.), so the whole regex now needs to see the same character throughout.

[–] [email protected] 1 points 5 days ago

The answer says "any character" not "any characters", so it is still correct.

load more comments (16 replies)