我有以下网址:
文件1.txt
http://www.stackoveflow.com/dog/cat/rabbit/hamster/
192.168.192.168/lion/tiger/elephant/
文件2.txt
HELLO
GOODBYE
我试图实现的输出:
http://www.stackoveflow.com/dogHELLO/cat/rabbit/hamster/
http://www.stackoveflow.com/dog/catHELLO/rabbit/hamster/
http://www.stackoveflow.com/dog/cat/rabbitHELLO/hamster/
http://www.stackoveflow.com/dog/cat/rabbit/hamsterHELLO/
http://www.stackoveflow.com/dog/cat/rabbit/hamster/HELLO
http://www.stackoveflow.com/dogGOODBYE/cat/rabbit/hamster/
http://www.stackoveflow.com/dog/catGOODBYE/rabbit/hamster/
http://www.stackoveflow.com/dog/cat/rabbitGOODBYE/hamster/
http://www.stackoveflow.com/dog/cat/rabbit/hamsterGOODBYE/
http://www.stackoveflow.com/dog/cat/rabbit/hamster/GOODBYE
192.168.192.168/lionHELLO/tiger/elephant/
192.168.192.168/lion/tigerHELLO/elephant/
192.168.192.168/lion/tiger/elephantHELLO/
192.168.192.168/lion/tiger/elephant/HELLO
192.168.192.168/lionGOODBYE/tiger/elephant/
192.168.192.168/lion/tigerGOODBYE/elephant/
192.168.192.168/lion/tiger/elephantGOODBYE/
192.168.192.168/lion/tiger/elephant/GOODBYE
如您所见,字符串HELLO
并GOODBYE
在每个斜线之后插入,如果斜线之后已经有一个字符串,它将在其后附加HELLO
and GOODBYE
(例如http://www.stackoveflow.com/dogHELLO/cat/rabbit/hamster/
,依此类推)。
我试过的
use strict;
use warnings;
my @f1 = do {
open my $fh, '<', 'FILE1.txt';
<$fh>;
};
chomp @f1;
my @f2 = do {
open my $fh, '<', 'FILE2.txt';
<$fh>;
};
chomp @f2;
for my $f1 (@f1) {
my @fields = $f1 =~ m{[^/]+}g;
for my $f2 (@f2) {
for my $i (0 .. $#fields) {
my @new = @fields;
$new[$i] .= $f2;
print qq{/$_/\n}, for join '/', @new;
}
print "\n\n";
}
}
#courtesy of Borodin
但是,此代码不适合在部分中包含斜杠的 url,因为它们在不应该做的时候http://
被替换。http:HELLO/
如果已经没有字符串,它也不会放在斜杠之后HELLO
或斜杠之后,例如GOODBYE
http://www.stackoveflow.com/dog/cat/rabbit/hamster/<--SHOULD PUT HELLO AFTER THIS SLASH AS WELL BUT DOSN'T
看来此代码删除然后重新插入带有 FILE2.txt 字符串的斜杠,而不是插入HELLO
并GOODBYE
在正确的位置开始。
我的问题
是否有更好的方法来实现我需要的输出,或者我可以对现有代码做些什么来解决上述问题?
非常感谢您的帮助,非常感谢