我正在使用正则表达式来匹配下面给出的示例Hl7消息的格式:
MSH|^~\`|674|GHC|SISRL|PAML|20060922162830|L674-200609221628310220|ORU^R01|ORU000016168|P|2.3|||AL|AL
PID|1|000-0000|||””|1922974|151-76-5760|||||||||||N
PV1|1|2|||||||| ||||||N|| ||
ORC|RE||2060059||||^^^200707061707^^ ||200707051013|DIONA |||””|||1007
OBR|||2060059|999991^Knee MRI WO| |200707061707|200707061621|200707061707||||””|””|||
OBX|1|TX|||PROCEDURE: MRI OF THE LEFT KNEE WITHOUT CONTRAST||||||F
$ filecontents保留上述消息。
$filecontents=~/^(.*?\rOBX\|.*?\|.*?\|.*?\|.*?\|)(.*?)(.*?)/;
$begin=$1;
$end=$3;
$content=$2;
3个变量的预期结果应如下:
$begin = "MSH|^~\`|674|GHC|SISRL|PAML|20060922162830|L674-200609221628310220|ORU^R01|ORU000016168|P|2.3|||AL |AL
PID|1|000-0000|||””|1922974|151-76-5760|||||||||||N
PV1|1|2|||||||| ||||||N|| ||
ORC|RE||2060059||||^^^200707061707^^ ||200707051013|DIONA |||””|||1007
OBR|||2060059|999991^Knee MRI WO| |200707061707|200707061621|200707061707||||””|””|||
OBX|1|TX|||";
$end = "||||||F";
$content = "PROCEDURE: MRI OF THE LEFT KNEE WITHOUT CONTRAST";
但我不是输出。有人能告诉我正则表达式有什么问题吗?
答案 0 :(得分:1)
你可能想要:
$filecontents =~ /(.*?^OBX\|.*?\|.*?\|.*?\|.*?\|)(.*?)\|(.*?)/ms;
关键变化是:
/s
修饰符,以便任何字符.
都匹配换行符。\r
修饰符,将^
更改为/m
。在完整的脚本中:
#!/usr/bin/perl -w
use strict;
use warnings;
my $data = do { local $/; <DATA> };
$data =~ /(.*?^OBX\|.*?\|.*?\|.*?\|.*?\|)(.*?)\|(.*?)/ms;
my ($begin, $content, $end) = ($1, $2, $3);
print "$content\n";
__DATA__
MSH|^~\`|674|GHC|SISRL|PAML|20060922162830|L674-200609221628310220|ORU^R01|ORU000016168|P|2.3|||AL|AL
PID|1|000-0000|||””|1922974|151-76-5760|||||||||||N
PV1|1|2|||||||| ||||||N|| ||
ORC|RE||2060059||||^^^200707061707^^ ||200707051013|DIONA |||””|||1007
OBR|||2060059|999991^Knee MRI WO| |200707061707|200707061621|200707061707||||””|””|||
OBX|1|TX|||PROCEDURE: MRI OF THE LEFT KNEE WITHOUT CONTRAST||||||F
输出:
PROCEDURE: MRI OF THE LEFT KNEE WITHOUT CONTRAST
答案 1 :(得分:1)
试试这个,它只是错过了$。
#!/usr/bin/perl
use strict;
use warnings;
my $data = do { local $/; <DATA> };
$data =~ /(.*?^OBX\|.*?\|.*?\|.*?\|.*?\|)(.*?)\|(.*?)$/ms;
my ($begin, $content, $end) = ($1, $2, $3);
print "BEGIN: $begin\n";
print "CONTENT: $content\n";
print "END: $end\n";
__DATA__
MSH|^~\`|674|GHC|SISRL|PAML|20060922162830|L674-200609221628310220|ORU^R01|ORU000016168|P|2.3|||AL|AL
PID|1|000-0000|||""|1922974|151-76-5760|||||||||||N
PV1|1|2|||||||| ||||||N|| ||
ORC|RE||2060059||||^^^200707061707^^ ||200707051013|DIONA |||""|||1007
OBR|||2060059|999991^Knee MRI WO| |200707061707|200707061621|200707061707||||""|""|||
OBX|1|TX|||PROCEDURE: MRI OF THE LEFT KNEE WITHOUT CONTRAST||||||F
输出
BEGIN: MSH|^~\`|674|GHC|SISRL|PAML|20060922162830|L674-200609221628310220|ORU^R01|ORU000016168|P|2.3|||AL|AL
PID|1|000-0000|||""|1922974|151-76-5760|||||||||||N
PV1|1|2|||||||| ||||||N|| ||
ORC|RE||2060059||||^^^200707061707^^ ||200707051013|DIONA |||""|||1007
OBR|||2060059|999991^Knee MRI WO| |200707061707|200707061621|200707061707||||""|""|||
OBX|1|TX|||
CONTENT: PROCEDURE: MRI OF THE LEFT KNEE WITHOUT CONTRAST
END: |||||F