My string is: $string = "hello = 1234ab;cat;dog;4892"; #### my $semiColon=2; if ( $string =~ /hello.*=(\s*.*;\s*){$semiColon}(\s*.*;\s*).*/i ) my $substring = $2;