English 中文(简体)
全球独立价值补贴
原标题:Global Regex Substitution with Unique Arbitrary Values

I have one huge HTML files with many links i.e. <a href="...">. I need to substitute each href with a unique arbitrary value. So, after substitution the first link will be <a href="http://link1">, second link <a href="http://link2">, and so on.

Can we do this using a regex? Or, do I need to write a small script to scan over the file? Ideally, the solution will be a Perl or bash script (not something proprietary).

感谢。

最佳回答

Perl或许是你最好的灯塔,但我不想试图在一席(甚至不可能)。 我认为,这很短,因为你可以做文字,同时仍然可以读懂:

#!/usr/bin/perl
$link = 1;
while(<>) {
    $link++ while( s/href="(?!linkd)[^"]*"/href="link$link"/ );
    print;
}

接着请它:

./thatScript.pl inputFile.html > newInputFile.html

它将审查每一条输入线和每一条<代码>href=> <>/code>,取而代之的是编号链接和编号。 还有一个消极的 look头来避免不断替换相同的<代码>href。

http://www.hchr.org。 就伤者而言,你将如何把上述内容排入一个双线:

perl -pe  $link++ while( s/href="(?!linkd)[^"]*"/href="link$link"/ )  inFile.html > outFile.html

问题回答

未经测试:

perl -pe  s{(href=")[^"]+}{$1 . "http://link" . ++$count}ge  filename > newfile




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签