[英]perl regex to read contents between double quotes
I have a file which contains information something like this: 我有一个文件,其中包含类似以下信息:
TAG1 "file1.txt"
some additional lines 一些额外的行
TAG2 "file2.txt"
some more lines 一些行
TAG3 "file3.txt".
Now, I want to read what is inside the double quotes and assign it to variable ( something like $var1 = file1.txt
$var2 = file2.txt
$var3 = fil3.txt)
. 现在,我想阅读双引号内的内容并将其分配给变量(类似
$var1 = file1.txt
$var2 = file2.txt
$var3 = fil3.txt)
。 Can anyone guild me how to do this.? 谁能告诉我该怎么做?
You could achieve your goal by 您可以通过实现目标
using regular expression 使用正则表达式
my @files; while (my $line = <>) { if (m/"([^"]+)"/) { push @files, $1; } }
using split()
使用
split()
my @files; while (my $line = <>) { my (undef, $file, undef) = split /"/, $line, 3; push @files, $file; }
Except for the period after "file3.txt".
除了
"file3.txt".
之后的时间段"file3.txt".
(which I suspect is a artifact from posting the question), your data appears to be a CSV file with tabs. (我怀疑这是发布问题的产物),您的数据似乎是带有标签的CSV文件。
If that's the case, I advise you to parse the file with Text::CSV
如果是这种情况,我建议您使用
Text::CSV
解析文件
use strict;
use warnings;
use autodie;
use Text::CSV;
my $csv = Text::CSV->new ( { sep_char => "\t" } )
or die "Cannot use CSV: ".Text::CSV->error_diag ();
my @files;
open my $fh, '<', 'file.csv';
while ( my $row = $csv->getline( $fh ) ) {
push @files; $row->[1];
}
$csv->eof or $csv->error_diag();
close $fh;
print "@files";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.