给定一个url,以下正则表达式可以在URL中的某些点处插入/替换单词。Perl正则表达式在特定位置插入/替换字符串
代码:
#!/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__
与HELLO
单词的第一个例子URL)给出的输出:
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/ra bbit/HELLO'两次? – ikegami 2013-03-01 16:34:41
@ikegami - 很好的问题,我希望它不会重复,我把它留在问题中,让其他人可以理解我想要更容易实现的输出类型,谢谢 – 2013-03-01 16:40:02
**这可能不是正则表达式的工作,而是使用您选择语言的现有工具。**您使用的是哪种语言?您可能不想使用正则表达式,而是使用已编写,测试和调试的现有模块。 如果您使用PHP,您需要['parse_url'](http://php.net/manual/en/function.parse-url.php)函数。 如果您使用Perl,您需要['URI'](http://search.cpan.org/dist/URI/)模块。 如果您使用的是Ruby,请使用['URI'](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/uri/rdoc/URI.html)模块。 – 2013-03-01 17:27:49