我有以下代码:
#!/usr/bin/perl
use strict;
use warnings;
use URI qw( );
my @insert_words = qw( HELLO );
while (<DATA>) {
chomp;
my $url = URI->new($_);
my $path = $url->path();
for (@insert_words) {
# Use package vars to communicate with /(?{})/ blocks.
my $insert_word = $_;
local our @paths;
$path =~ m{
^(.*/)([^/]*)((?:/.*)?)\z
(?{
push @paths, "$1$insert_word$2$3";
if (length($2)) {
push @paths, "$1$insert_word$3";
push @paths, "$1$2$insert_word$3";
}
})
(?!)
}x;
for (@paths) {
$url->path($_);
print "$url\n";
}
}
}
__DATA__
http://www.bagandboxfactory.com/index.php?route=checkout/
http://www.stackoverflow.com/dog/cat/rabbit/
http://www.superuser.co.uk/dog/cat/rabbit/hamster/
目前,上面的代码非常适用stackoverflow and superuser
于__DATA__
. 它为stackoverflow
url 提供以下输出:
http://www.stackoverflow.com/dog/cat/rabbit/HELLO
http://www.stackoverflow.com/dog/cat/HELLOrabbit/
http://www.stackoverflow.com/dog/cat/HELLO/
http://www.stackoverflow.com/dog/cat/rabbitHELLO/
http://www.stackoverflow.com/dog/HELLOcat/rabbit/
http://www.stackoverflow.com/dog/HELLO/rabbit/
http://www.stackoverflow.com/dog/catHELLO/rabbit/
http://www.stackoverflow.com/HELLOdog/cat/rabbit/
http://www.stackoverflow.com/HELLO/cat/rabbit/
http://www.stackoverflow.com/dogHELLO/cat/rabbit/
如您所见,HELLO
当遇到斜杠 (/) 时,它会在特定位置插入字符串。
我遇到的问题:
当在 url 中找到等号 (=) 时,我希望发生同样的事情。
举http://www.bagandboxfactory.com/index.php?route=checkout
个例子,我希望输出给我以下信息:
http://www.bagandboxfactory.com/index.php?route=HELLOcheckout/ <- puts HELLO before the string after the equals
http://www.bagandboxfactory.com/index.php?route=HELLO/ <- replaces the string after the equals with HELLO
http://www.bagandboxfactory.com/index.php?route=checkoutHELLO/ <- puts HELLO after the string that is after the equals
我认为将正则表达式从
^(.*/)([^/]*)((?:/.*)?)\z
到
^(.*[/=])([^/=]*)((?:[/=].*)?)\z
会工作,但它不会。
我需要对正则表达式进行什么更改才能做到这一点?
非常感谢您的帮助,非常感谢
__更新_ _ _
它需要能够处理多个参数,例如,如果我有 url http://www.example.com/dog/cat=2&foo=5
,我应该得到的输出如下:
http://www.example.com/HELLOdog/cat=2&foo=5
http://www.example.com/HELLO/cat=2&foo=5
http://www.example.com/dogHELLO/cat=2&foo=5
http://www.example.com/dog/cat=HELLO2&foo=5
http://www.example.com/dog/cat=HELLO&foo=5
http://www.example.com/dog/cat=2HELLO&foo=5
http://www.example.com/dog/cat=2&foo=HELLO5
http://www.example.com/dog/cat=2&foo=HELLO
http://www.example.com/dog/cat=2&foo=5HELLO
我已经拥有的代码可以正常工作,并且对它在 url 中遇到的每个斜线都执行此操作,但我现在希望它在遇到=
url 中的时也这样做(或我选择在正则表达式中指定的任何其他字符例如 [/=@-])。