How on Earth does ^.?$|^(..+?)1+$ produce primes?

From Standup Mathematician.

Take your personal data back with Incogni! Use code standupmaths at the link below and get 60% off an annual plan: https://incogni.com/standupmaths

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

Props will be for sale here: https://www.ebay.co.uk/usr/standupmaths

Fantastic blog post by Illya Gerasymchuk. "Demystifying The Regular Expression That Checks If A Number Is Prime" https://illya.sh/the-codeumentary-blog/regular-expression-check-if-number-is-prime/

Thanks to everyone who has sent in history about this regex. Seems it was written ≥25 years ago by a Perl programmer named Abigail.

Cheers to viewer Dave Cross who found this earliest-known mention in a 2000 blog post: http://test.neilk.net/blog/2000/06/01/abigails-regex-to-test-for-prime-numbers/

Thanks to viewers Chris Lawrence and Jimmy Diep for suggesting the topic.

Huge thanks to my Patreon supporters. They keep me supplied with all the primes I need. https://www.patreon.com/standupmaths

CORRECTIONS
– Several people have corrected me that regex is zero indexed it’s just that refers to the entire matched string.
– Mike Salisbury ands others have pointed out that the left-most column in the table of repeated wild cards shouldn’t be included. Otherwise the length of the string could match a single prime length.
– At 6:07 I say "Begins and end with an m" when I meant ends with an “s”. No one has corrected that mistake with more vigour than viewer Timur Sultanov.
– There is some concern about my pronunciation of rejex.
– Let me know if you spot anything else!

Filming, VFX and editing by Alex Genn-Bash
Written and performed by Matt Parker
Extra material by Sam Hartburn
Number yeeting by Lucie Green
Spooky lighting and producing by Nicole Jacobus
Music by Howard Carter
Design by Simon Wright and Adam Robinson

MATT PARKER: Stand-up Mathematician
Website: http://standupmaths.com/