使用 Perl 的正则表达式查找项目
Using Perl's regular expression to find items
我有一个设置的 Perl 脚本,用于从文本文件中检索数据,但不会显示的是任何以星号开头的内容。这是我的代码:
while(<INFILE>) {
chomp $_;
if (m/^\s*$/) {
next;
}
my @fields = split(/\ /,$_);
my @output;
foreach my $field(@fields) {
if($field =~ /^\*?[ABMQRWY][A-Z0-9]{4}235 / ) {
push @output,$field;
}
}
}
我的 if 语句抓取所有 8 个字符长的内容,但有时它不会抓取作业前面的 *。我如何包括它?我知道我需要逃避它,但不确定把它放在哪里。
以下是我提取的一些数据示例:
W50DW235 DW
M50DW235 DW
Q0608235 08
APJ40235 40
M3515235 15
M34DW235 DW
M3408235 08
RES08235 08
BSP20235 20
W1208235 08
B3008235 08
这是文件中的原始行:
18122/0655 18122/0700 W50DW235 DW LEV001 002 D50DW235
18122/0735 18122/0740 M50DW235 DW LEV002 002 W50DW235
它不会从带星号的行是这样的:
18123/0300 18123/0400 *D1708235 08 LEV001 001
为了提取数据,我使用以下方法捕获带有两个 space 中断的项目:
my @fields = split(/\ /,$_);
与星号无关!带星号的项目是 *D1708235
,但您的正则表达式要求第一个字母字符是 [ABMQRWY]
,其中不包括 D
.
也就是说,这是一个经过清理的示例:
my @output;
while(<>) {
chomp; # Don't need to specify $_ - it's the default
next if /^\s*$/; # Ditto, and the single-line form for readability
#print; # If you want to see what it's doing
my @fields = split; # Split $_ on whitespace-separated fields
foreach my $field (@fields) {
#print "-$field-\n";
push @output, $field # Again, single-line form
if $field =~ /^\*?[ABDMQRWY][A-Z0-9]{4}235/;
} # ^ the missing link ^ no trailing whitespace
}
print "Results:\n", join("\n", @output), "\n";
我从 $field
正则表达式中删除了尾随空格,因为 split
将生成既没有前导空格也没有尾随空格的字段。
输入:
W50DW235 DW
M50DW235 DW
Q0608235 08
APJ40235 40
M3515235 15
M34DW235 DW
M3408235 08
RES08235 08
BSP20235 20
W1208235 08
B3008235 08
18122/0655 18122/0700 W50DW235 DW LEV001 002 D50DW235
18122/0735 18122/0740 M50DW235 DW LEV002 002 W50DW235
18123/0300 18123/0400 *D1708235 08 LEV001 001
输出:
Results:
W50DW235
M50DW235
Q0608235
APJ40235
M3515235
M34DW235
M3408235
RES08235
BSP20235
W1208235
B3008235
W50DW235
D50DW235
M50DW235
W50DW235
*D1708235
我有一个设置的 Perl 脚本,用于从文本文件中检索数据,但不会显示的是任何以星号开头的内容。这是我的代码:
while(<INFILE>) {
chomp $_;
if (m/^\s*$/) {
next;
}
my @fields = split(/\ /,$_);
my @output;
foreach my $field(@fields) {
if($field =~ /^\*?[ABMQRWY][A-Z0-9]{4}235 / ) {
push @output,$field;
}
}
}
我的 if 语句抓取所有 8 个字符长的内容,但有时它不会抓取作业前面的 *。我如何包括它?我知道我需要逃避它,但不确定把它放在哪里。
以下是我提取的一些数据示例:
W50DW235 DW
M50DW235 DW
Q0608235 08
APJ40235 40
M3515235 15
M34DW235 DW
M3408235 08
RES08235 08
BSP20235 20
W1208235 08
B3008235 08
这是文件中的原始行:
18122/0655 18122/0700 W50DW235 DW LEV001 002 D50DW235
18122/0735 18122/0740 M50DW235 DW LEV002 002 W50DW235
它不会从带星号的行是这样的:
18123/0300 18123/0400 *D1708235 08 LEV001 001
为了提取数据,我使用以下方法捕获带有两个 space 中断的项目:
my @fields = split(/\ /,$_);
与星号无关!带星号的项目是 *D1708235
,但您的正则表达式要求第一个字母字符是 [ABMQRWY]
,其中不包括 D
.
也就是说,这是一个经过清理的示例:
my @output;
while(<>) {
chomp; # Don't need to specify $_ - it's the default
next if /^\s*$/; # Ditto, and the single-line form for readability
#print; # If you want to see what it's doing
my @fields = split; # Split $_ on whitespace-separated fields
foreach my $field (@fields) {
#print "-$field-\n";
push @output, $field # Again, single-line form
if $field =~ /^\*?[ABDMQRWY][A-Z0-9]{4}235/;
} # ^ the missing link ^ no trailing whitespace
}
print "Results:\n", join("\n", @output), "\n";
我从 $field
正则表达式中删除了尾随空格,因为 split
将生成既没有前导空格也没有尾随空格的字段。
输入:
W50DW235 DW
M50DW235 DW
Q0608235 08
APJ40235 40
M3515235 15
M34DW235 DW
M3408235 08
RES08235 08
BSP20235 20
W1208235 08
B3008235 08
18122/0655 18122/0700 W50DW235 DW LEV001 002 D50DW235
18122/0735 18122/0740 M50DW235 DW LEV002 002 W50DW235
18123/0300 18123/0400 *D1708235 08 LEV001 001
输出:
Results:
W50DW235
M50DW235
Q0608235
APJ40235
M3515235
M34DW235
M3408235
RES08235
BSP20235
W1208235
B3008235
W50DW235
D50DW235
M50DW235
W50DW235
*D1708235