in reply to check for power of a number with regex

Well, this works:

my $n = ...; my $b = ...; my @possibles; my $p = '1'; while ($p <= $n) { push @possibles, ('1' x $p); $p *= $b; } my ($pat) = map qr/$_/, join '|', @possibles; print( "$n: ", ('1' x $n) =~ /^$pat\z/ ?1:0, "\n" );

You can do almost all of the above inside the pattern, except for the $n <= $number check:

my $n = ...; my $b = ...; my $b_m1 = $b-1; my $pat = qr/(1|((?-2))\g{-1}{$b_m1})/; print( "$n: ", ('1' x $n) =~ /^$pat\z/ ?1:0, "\n" );

Without that check, the regex engine preemptively (and sometimes accurately) throws "Infinite recursion in regex".

Back to the drawing board.

Update: As best as I can tell, you'd need to be able to change the topic as follows:

local our $pat; $pat = qr/ 1 | (1+) \g{-1}{$b_m1} (?(?{ $^N =~ $pat })(?=)|(?!)) /x;

But that deosn't even work *with* (?{})!