Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture the match content of two different regexp in perl

Tags:

regex

perl

I am using a while loop with two separate regular expression

while(($string1=~m/(\d+)/igs)==($string2=~m/([^^]*?)\n+/igs)) {}

to store the value of the matching pattern of the $string1 i have used $temp1=$1,

How can I store the matching pattern of the $string2. Please give some suggestion.

like image 770
Balakumar Avatar asked Sep 02 '13 18:09

Balakumar


2 Answers

There might be more clever ways, but I'd just break them up into separate statements:

while (1) {
    $res1 = $string1=~m/(\d+)/igs;
    $temp1 = $1;
    $res2 = $string2=~m/([^^]*?)\n+/igs
    $temp2 = $1;
    last unless $res1 == $res2;
    ...
}

Just because it's perl you don't have to find the most terse, cryptic way to write something (that's what APL is for).

like image 22
Barmar Avatar answered Nov 15 '22 10:11

Barmar


my ($m1,$m2);
while (do{
  ($m1,$m2) = ();
  $m1 = $1 if $string1 =~ /(\d+)/igs;
  $m2 = $1 if $string2 =~ /([^^]*?)\n+/igs;
  defined $m1 == defined $m2;
}) {
  # print "$m1-$m2-\n";
}
like image 60
mpapec Avatar answered Nov 15 '22 09:11

mpapec