The author was intrigued by a unique piece of code that checks for prime numbers and breaks down a regular expression that filters out non-prime numbers in unary form. Both parts of the expression, ^.?$ and ^(..+?)1+$, must match the entire string to determine if the number is prime. The first part matches strings with 0 or 1 characters (representing numbers 0 and 1), while the second part focuses on matching multiples of increasing numbers, using capturing groups and backreferences to check if the string’s length is a multiple of the matched number. By analyzing how each part works, the author explains the algorithm’s effectiveness in identifying non-prime numbers.
https://illya.sh/the-codeumentary-blog/regular-expression-check-if-number-is-prime/