【发布时间】:2013-02-16 04:13:12
【问题描述】:
给定一个 url,以下正则表达式可以在 url 的某些点插入/替换单词。
代码:
#!/usr/bin/perl
use strict;
use warnings;
#use diagnostics;
my @insert_words = qw/HELLO GOODBYE/;
my $word = 0;
my $match;
while (<DATA>) {
chomp;
foreach my $word (@insert_words)
{
my $repeat = 1;
while ((my $match=$_) =~ s|(?<![/])(?:[/](?![/])[^/]*){$repeat}[^/]*\K|$word|)
{
print "$match\n";
$repeat++;
}
print "\n";
}
}
__DATA__
http://www.stackoverflow.com/dog/cat/rabbit/
http://www.superuser.co.uk/dog/cat/rabbit/hamster/
10.15.16.17/dog/cat/rabbit/
给出的输出(__DATA__ 中的第一个示例 url 带有 HELLO 字):
http://www.stackoverflow.com/dogHELLO/cat/rabbit/
http://www.stackoverflow.com/dog/catHELLO/rabbit/
http://www.stackoverflow.com/dog/cat/rabbitHELLO/
http://www.stackoverflow.com/dog/cat/rabbit/HELLO
我现在卡住的地方:
我现在想更改正则表达式,使输出如下所示:
http://www.stackoverflow.com/dogHELLO/cat/rabbit/
http://www.stackoverflow.com/dog/catHELLO/rabbit/
http://www.stackoverflow.com/dog/cat/rabbitHELLO/
http://www.stackoverflow.com/dog/cat/rabbit/HELLO
#above is what it already does at the moment
#below is what i also want it to be able to do as well
http://www.stackoverflow.com/HELLOdog/cat/rabbit/ #<-puts the word at the start of the string
http://www.stackoverflow.com/dog/HELLOcat/rabbit/
http://www.stackoverflow.com/dog/cat/HELLOrabbit/
http://www.stackoverflow.com/dog/cat/rabbit/HELLO
http://www.stackoverflow.com/HELLO/cat/rabbit/ #<- now also replaces the string with the word
http://www.stackoverflow.com/dog/HELLO/rabbit/
http://www.stackoverflow.com/dog/cat/HELLO/
http://www.stackoverflow.com/dog/cat/rabbit/HELLO
但我无法让它在一个正则表达式中自动执行此操作。
非常感谢您对此事的任何帮助,非常感谢
【问题讨论】:
-
你的意思是把
/dog/cat/rabbit/HELLO放两次吗? -
@ikegami - 好问题,我希望它不要重复,我把它留在问题中,以便其他人可以更轻松地理解我想要实现的输出类型,谢谢
-
我正在使用perl,谢谢你的建议,我不知道我没有使用URI模块是怎么过的,谢谢
-
@AndyLester - 我假设我需要使用 perl
URI::Escape和uri_unescape($url)在我的 arrayref 数据结构中以更“人类可读”的形式查看 URL,当它们被打印到 STDOUT 时,因为我的网址中的许多字符都是百分比编码的。但是,在实际使用LWP::UserAgent获取特定 url 时,最好使用uri_escape($url)对字符进行百分比编码
标签: regex string perl substitution string-substitution