#!/usr/bin/env perl use Modern::Perl; my $str = 'abc123efghijk'; my $i = 2; if( $str =~ /(\d{@{[$i+1]}})/ ){ say $1; } # output -> 123