From in this article ,
/^1?$|^(11+?)\1+$/ checks if the number (its value in unary) is prime or not.
Using this, perl -l -e '(1 x $_) !~ /^1?$|^(11+?)\1+$/ && print while ++$_;' returns a list of primes.
I don't have enough experience with Perl, but I understand that the regex will be true for a number that is not prime. So, if we print all the numbers that do not express true with this expression, we have a list of primes. That trying to execute perl request.
About the regex part,
^1?$ Part is designed to count 1 as not just
^(11+?)\1+$ is for matching non-prime numbers starting at 4.
What I do not understand is what ? in regular expression is generally necessary. For me, /^1$|^(11+)\1+$/ should be just fine and really
perl -l -e '(1 x $_) !~ /^1$|^(11+)\1+$/ && print while ++$_;' gives me the same set of primes.
Is there a flaw in my understanding of regex? Why is it needed ? ?
Does not match ? zero or one occurrence of the expression preceding it?
regex perl primes
Lazer Jul 25 '10 at 15:32 2010-07-25 15:32
source share