所以我有這個文件clip.txt僅包含:perl的SUBSTR在字符串中刪除兩個位置之間的一切
<a href="https://en.wikipedia.org/wiki/Kanye_West">Kanye West</a>,
<a href="http://en.wikipedia.org/wiki/Chris_Martin">Chris Martin</a>
現在我想,這樣我就結了,除去之間< ...>一切Kanye West,Christ Martin。
Kanye West,Christ Martin。
用Perl我有當前代碼:
#!/usr/local/bin/perl
$file = 'clip.txt';
open(FILE, $file);
@lines = <FILE>;
close(FILE);
$line = @lines[0];
while (index($line, "<") != -1) {
my $from = rindex($line, "<");
my $to = rindex($line, ">");
print $from;
print ' - ';
print $to;
print ' ';
print substr($line, $from, $to+1);
print '|'; // to see where the line stops
print "\n";
substr($line, $from, $to+1) = ""; //removes between lines
$counter += 1;
}
print $line;
所有的「打印」線是相當多餘的,但良好的進行調試。
現在結果就變成:
138 - 141 </a>
|
67 - 125 <a href="http://http://en.wikipedia.org/wiki/Chris_Martin">Chris Martin|
61 - 64 </a>, |
0 - 50 <a href="https://en.wikipedia.org/wiki/Kanye_West">|
Kanye West
首先劇本找到138 -141之間的位置,並將其刪除。 然後找到67 - 125,但它刪除67 - 137. 接下來它發現61 - 64,但它刪除61 - 66.
它爲什麼這樣做? 在底線上找到0 - 64,並完美刪除。所以我找不到這裏的邏輯。
一個soulution可能是使用HTML解析器的Perl –