我一直在研究这么久! 我会感激你的帮助......Perl提取文本
我的文档将是什么样子:
<text>
<text> command <+>= "stuff_i_need" <text>
<text>
<text> command <+>= stuff <text>
<text>
<text> command <+>= -stuff <text>
<text>
- 任何与纠结括号是可选
- 东西可以是任何东西(苹果,橘子,香蕉),但它是什么,我需要提取
- 命令固定
我码S ø远:
#!/usr/bin/env perl
use warnings;
use strict;
use Text::Diff;
# File Handlers
open(my $ofh, '>in.txt');
open(my $ifh, '<out.txt');
while (<$ifh>)
{
# Read in a line
my $line = $_;
chomp $line;
# Extract stuff
my $extraction = $line;
if ($line =~ /command \+= /i) {
$extraction =~ s/.*"(.*)".*/$1/;
# Write to file
print $ofh "$extraction\n";
}
}
您需要指定哪些运算符是有效的。在你的代码中你只能看到'+ =',但是在你的示例输入中你有'+ ='和'='。 'command' vs'command2'也是一样。当你想提取单词时,你需要指定确切的条件。 – TLP 2012-08-17 18:28:21
感谢提示@TLP。我做了一些更改,请让我知道是否有帮助。 – Ryan 2012-08-17 18:40:17