我一直在研究這麼久! 我會感激你的幫助......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