我正在尝试编写一个 Perl 脚本,该脚本将更改如下所示的行...
<li><em>01 – Chapters 1-4</em> – 00:14:36 <br />
...让它看起来像这样...
01 – Chapters 1-4
……没什么大不了的吧?我只是\(.*\)
在我的 Perl 脚本中做这样的事情:
#!/usr/bin/perl -w
use strict;
while(<DATA>) {
my $line = $_;
chomp($line);
if ( $line =~ /^<li>/ ) {
$line =~ s/<em>\(.*\)<\/em>/$1/g;
print "[" . $line . "]\n";
}
}
__DATA__
<li><em>01 – Chapters 1-4</em> – 00:14:36 <br />
<li><em>02 – Chapters 5-8</em> – 00:10:52 <br />
<li><em>03 – Chapters 9-14</em> – 00:19:16 <br />
<li><em>04 – Chapters 15-18</em> – 00:13:30 <br />
<li><em>05 – Chapters 19-22</em> – 00:17:01 <br />
<li><em>06 – Chapters 23-25</em> – 00:16:44 <br />
<li><em>07 – Chapter 26</em> – 00:10:35 <br />
red@ubuntu:~/scripts$ ./test.pl
当我运行脚本时,我得到这个输出......
[<li><em>01 – Chapters 1-4</em> – 00:14:36 <br />]
[<li><em>02 – Chapters 5-8</em> – 00:10:52 <br />]
[<li><em>03 – Chapters 9-14</em> – 00:19:16 <br />]
[<li><em>04 – Chapters 15-18</em> – 00:13:30 <br />]
[<li><em>05 – Chapters 19-22</em> – 00:17:01 <br />]
[<li><em>06 – Chapters 23-25</em> – 00:16:44 <br />]
[<li><em>07 – Chapter 26</em> – 00:10:35 <br />]
...我在这里做错了什么?
谢谢
更新:
感谢你的回复。他们非常有帮助。我已经把我的代码改成了这个......
red@ubuntu:~/scripts$ cat test.pl
#!/usr/bin/perl -w
use strict;
while(<DATA>) {
my $line = $_;
chomp($line);
if ( $line =~ /^<li>/ ) {
$line =~ s/<em>(.*)<\/em>/$1/g;
print "[" . $line . "]\n";
}
}
__DATA__
<li><em>01 – Chapters 1-4</em> – 00:14:36 <br />
<li><em>02 – Chapters 5-8</em> – 00:10:52 <br />
<li><em>03 – Chapters 9-14</em> – 00:19:16 <br />
<li><em>04 – Chapters 15-18</em> – 00:13:30 <br />
<li><em>05 – Chapters 19-22</em> – 00:17:01 <br />
<li><em>06 – Chapters 23-25</em> – 00:16:44 <br />
<li><em>07 – Chapter 26</em> – 00:10:35 <br />
...但仍然没有得到我想要的输出我得到这个...
red@ubuntu:~/scripts$ ./test.pl
[<li>01 – Chapters 1-4 – 00:14:36 <br />]
[<li>02 – Chapters 5-8 – 00:10:52 <br />]
[<li>03 – Chapters 9-14 – 00:19:16 <br />]
[<li>04 – Chapters 15-18 – 00:13:30 <br />]
[<li>05 – Chapters 19-22 – 00:17:01 <br />]
[<li>06 – Chapters 23-25 – 00:16:44 <br />]
[<li>07 – Chapter 26 – 00:10:35 <br />]
...看起来<em>
and</em>
被删除了,但我只想要<em>
and之间的文本</em>
。