m[ ^ (a*) (??{ 'b' x ( length($1) + 1 ) }) $ ]x and print "'$_'\n" for qw[ ab abb abbb aabb aabbb aabbbb xaabbby ]; 'abb' 'aabbb'