我有一个包含许多值的文件。一旦找到 REGEX,我试图只提取文件中匹配的所需行。文件.txt
......
......
TRIP (
?name "model"
?prompt "Model name"
?defamount "USD100"
?type "adventure"
?display "no"
?photos "800"
)
TRIP (
?name "macro"
?prompt "macro model name"
?defamount "USD500"
?type "adventure"
?display "no"
?photos "1200"
)
TRIP(
?name "description"
?prompt "description"
?defamount "USD400"
?type "historical"
?display "yes"
?photos "900"
)
.......
........
.....
.......
......
我想提取带有“名称和型号”的行,然后是“名称和描述”以及这些行我想提取第一个“defamount”,一旦“名称和型号”的行匹配,然后是“名称和描述”。我尝试了循环,但没有成功
use strict;
open FILE, "<File.txt";
open FILE2, ">>data1.l";
while (my $string = <FILE>) {
if ($string =~ m/^ CELL/ig) {
print FILE2 $string;
}
elsif ($string =~ m/"model"/i) {
print FILE2 $string;
}
elsif ($string =~ m/defamount/o) {
print FILE2 $string;
}
elsif($string =~ m/"description"/i) {
print FILE2 $string;
}
elsif($string =~ m/defamount/o) {
print FILE2 $string;
}
}
close FILE;
close FILE2;
这给了我文件中的所有“defamount”行,但我只想要匹配上述正则表达式后的第一个 defamount