1

I'm relatively new to python and coding in general and have been delving into some example code to broaden my horizons and came across a prime number identifier that uses this regular expression.

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

I have managed to ascertain that it uses bitwise operators but am struggling to grasp the full function of this expression, could someone please explain it in more layman's terms?

Code for reference

import re


def is_prime(n):
    return re.match(r'^1?$|^(11+?)\1+$', '1' * n) is None


N = int(50)
M = 100
primeList = list()

while len(primeList) < N:
    primeList += filter(is_prime, range(M - 100, M))
    M += 100

print(primeList[:N]) 

0 Answers0