in reply to Re: how to search and replace the match with specific number of times in a string
in thread how to search and replace the match with specific number of times in a string

Note that there is a warning (at least as of the current documentation) that the  (*ACCEPT) backtracking control verb is "... highly experimental [and] is not recommended for production code."

A slightly different take:

>perl -wMstrict -le "my $N = 3; my $string = 'XXXXXaXXbXXXc'; $string =~ s{ X (?(?{ $N and $N-- }) | (*FAIL)) }{Xy}xmsg; print $string; " XyXyXyXXaXXbXXXc

See Special Backtracking Control Verbs in perlre.