[英]Perl regex to match pair of strings
Here is my regex link: https://www.regex101.com/r/jksOY7/1 这是我的正则表达式链接: https : //www.regex101.com/r/jksOY7/1
I am getting matches with my regex. 我正在用正则表达式进行匹配。 But the problem is output.
但是问题是输出。 I am getting six matches.
我要打六场比赛。 If i use while loop, Then i am getting continue output.
如果我使用while循环,那么我将继续输出。
But i need to relate as below using Perl. 但是我需要使用Perl进行如下关联。
[{
match1=> 'GEL-111111111-22222',
match2=> 'UP Ej011.2223'
},
{
match1=> 'FPL-222222222-33333',
match2=> 'UP Ek04112.2883'
},
{
match1=> 'HGL-333333333-44444',
match2=> 'UP Eg04213.2323'
}]
The below Perl code is just returning continue output. 下面的Perl代码只是返回continue输出。
while($tunnel =~ /(?![\-]+)([A-Z]{3,5}-[0-9\-\_]+)|(UP\s+([A-Za-z0-9\/]+.[0-9]+))/g){
print "\n";
print Dumper $1;
print "\n";
print "\n";
print Dumper $3;
print "\n";
}
Could someone please help on that to get output as above? 有人可以帮忙获得上述输出吗?
My Perl is a little rusty. 我的Perl有点生锈。
Expanded regex 扩展正则表达式
(?s)
( [A-Z]{3,5} - [0-9_-]+ ) # (1)
(?:
(?! -------------- )
.
)*?
( # (2 start)
UP \s+
[A-Za-z0-9/]+ \. [0-9]+
) # (2 end)
Code 码
use strict;
use warnings;
use Data::Dumper;
$/ = undef;
my $teststring = <DATA>;
my @ary;
while ( $teststring =~ /(?s)([A-Z]{3,5}-[0-9_-]+)(?:(?!--------------).)*?(UP\s+[A-Za-z0-9\/]+\.[0-9]+)/g )
{
my $href = {};
$href->{'group1'} = $1;
$href->{'group2'} = $2;
push @ary, $href;
}
print Dumper \@ary;
__DATA__
Group Name FT Description FT Description DT
------------------------ ----------------------------- -----------------------------
GEL-111111111-22222
IWRJWERODSF_WERONSDFELEO_23232
UP Ej011.2223 DWD 56.67.22.234 1222332432
DWW
----------------------------------------------------------------------------------------
FPL-222222222-33333
WERWERDSFSD_PJRRINSDFKEW_10022
UP Ek04112.2883 DWD 78.24.85.344 1232332432
DW
----------------------------------------------------------------------------------------
HGL-333333333-44444
SDFOWERNOWE_SDFONWERIODS_232323
UP Eg04213.2323 DWD Eg04213.2323 DWD
----------------------------------------------------------------------------------------
Output 产量
$VAR1 = [
{
'group1' => 'GEL-111111111-22222',
'group2' => 'UP Ej011.2223'
},
{
'group1' => 'FPL-222222222-33333',
'group2' => 'UP Ek04112.2883'
},
{
'group1' => 'HGL-333333333-44444',
'group2' => 'UP Eg04213.2323'
}
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.