my $semiColon=2; if ( $string =~ /hello.*=(\s*.*;\s*){$semiColon}(\s*.*;\s*).*/i ) my $substring = $2;