[英]ACARS message parsing
I need to parse ACARS messages to XML format. 我需要将ACARS消息解析为XML格式。
There is simple messages: 有简单的消息:
RX_IDX: 13
ACARS mode: O, message label: 5V
ACARS ML description: VDL switch advisory
Aircraft reg: .EI-EUX, flight id: UN0323
Block id: 57, msg. no: S91A
Message content:-
----------------------------------------------------------[05/05/2013 08:58]
RX_IDX: 14
ACARS mode: 2, message label: 1L
ACARS ML description: Off message
Aircraft reg: .D-AIRO, flight id: LH1490
Aircraft vendor: Airbus, short type: A321, full type: A321-131, cn: 0563
Carrier IATA: LH, ICAO: DLH, remarks: Lufthansa
Airlines: Lufthansa
Block id: 56, msg. no: M03A
Message content:-
00002216743GO,X,55655
----------------------------------------------------------[05/05/2013 09:24]
Each message starts with RX_IDX and ends with date (eg. [05/05/2013 09:24]). 每个消息均以RX_IDX开头,并以日期结尾(例如[05/05/2013 09:24])。
I found perl script, but it doesn't recognize attributes after commas. 我找到了perl脚本,但是逗号后不能识别属性。
#!/usr/local/bin/perl
use strict;
use warnings;
my @keys = (
'RX_IDX',
'ACARS mode',
'message label',
'ACARS ML description',
'Aircraft reg',
'flight id',
'Aircraft vendor',
'short type',
'full type',
'cn',
'Carrier IATA',
'ICAO',
'remarks',
'Airlines',
'Block id',
'msg. no',
'Message content'
);
my( %keys, %tags );
$keys{$_} = 1 for @keys;
$tags{$_} = $_ . '' for @keys;
$tags{$_} =~ s/ /_/g for @keys;
my $file = 'data8.txt';
open( my $fh, '<', $file) or die("Can't open $file: $!");
my %record = map { $_, '' } @keys;
while( my $line = <$fh> ) {
chomp($line);
if( $line =~ m{ \A (.+?) : \s* (\S+) }x ) {
$record{$1} = $2 if $keys{$1};
if( $1 eq $keys[$#keys] ) {
print "<Message>\n";
print "<$tags{$_}>$record{$_}</$tags{$_}>\n" for @keys;
print "</Message>\n";
%record = map { $_, '' } @keys;
}
}
}
Regards 问候
The problem is that the if
condition of the regex only will match once for each line. 问题是正则表达式的
if
条件仅对每行匹配一次。 Try to match the regular expression until if fails in a while loop. 尝试匹配正则表达式,直到在while循环中失败为止。 I added the
\\G
assertion when in next loop it will begin when left last time. 我在下一个循环中添加了
\\G
断言,它将在上次离开时开始。 Also changed it a little bit to avoid matching at the beginning of line ( \\A
) and added a possible match of a comma at the end, it will be like this (I copied only relevant part of code): 还作了一些更改以避免在行(
\\A
)开头匹配,并在结尾添加了逗号的可能匹配,就像这样(我只复制了代码的相关部分):
while( my $line = <$fh> ) {
chomp($line);
while ( $line =~ m{ \G \s* (.+?) \s* : \s* ([^,]+) \s* (?:,|$) }xg ) {
$record{$1} = $2 if $keys{$1};
if( $1 eq $keys[$#keys] ) {
...
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.