[英]Perl Regular Expression Pattern
我有这样一些数据:
TYPE: Travel
ADDRESS
Barcelona
Paris
因此,地址可以是1或很多(我需要丢弃ADDRESS并仅获取那些城市)。 由于某些原因,我的解析失败(仅打印“ ADDRESS”)无法产生正确的结果。我错过了什么吗?
elsif (/^ADDRESS/) {
my @address_t = split /[no matter what i put,only ADDRESS is printed]+/, $_;
shift @address_t; #is this how i will discard ADDRESS ?
foreach my $address (@address_t) {
@address_names = ($address);
}
我认为正则表达式应该被换行,空格?
这就是我处理TYPE的方式:
elsif (/^TYPE/) {
my @type_t = split '\s', $_;
$type = $type_tmp[1];
print "$type" ; #to test, but i have a hashmap which i load them in and print at the end of the file.
谢谢
尝试这样的事情:
如果前一行匹配/^ADDRESS/
,它将打印行。 让我知道您是否要停止,我可以调整...
use warnings;
use strict;
my $current_line = "";
my $line_count = 0;
while (<IN>){
chomp;
my $previous_line = $current_line;
$current_line = $_;
if ($previous_line =~ /^ADDRESS/ or $line_count > 0 ){
$line_count++;
print "$current_line\n"
}
}
use warnings;
use strict;
while(<DATA>) {
if (/^ADDRESS/) { # if line contains ADDRESS then read addresses
while (<DATA>) { # ... in a loop
last if !/^ +/; # until we find a non-indented line
print $_; # here you can push $_ to a list
}
}
if ($_ && /^TYPE/) { # a TYPE after address can be processed now
# stuff
}
}
__DATA__
TYPE: Travel
ADDRESS
Barcelona
Paris
TYPE: Travel
ADDRESS
Barcelona
Paris
生产:
Barcelona
Paris
Barcelona
Paris
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.