Perl正则表达式匹配字符串对

时间:2022-01-08 08:56:21

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代码只是返回继续输出。

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?

有人可以请求帮助,以获得上述输出?

1 个解决方案

#1


0  

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'
          }
        ];

#1


0  

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'
          }
        ];