Search code examples
regexperlgrep

Perl regex to match pair of strings


Here is my regex link: 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.

But i need to relate as below using 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.

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";
}

How can I get output as above?


Solution

  • My Perl is a little rusty.

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