简体   繁体   中英

perl multiple regex match over multiple lines

I'm struggling with trying to capture multiple matches within a group of lines in a text file.

The data takes on a bunch of forms like

AO22_X1N_A9PP96CTS_C24 SYN_INC_187 ( .A0 ( test_so6 ) , .A1 ( n2218 ) , .B0 ( U_PAUSEdata_ff_int_28_ ) , .B1 ( n2 ) , .Y ( n2597 ) ) ;

NAND3_X1R_A9PP96CTUL_C16 SYN_INC_154 ( .A ( n1563 ) , .B ( U_PAUSEwcnt ) , .C ( n1640 ) , .Y ( n1467 ) ) ;

The first piece is a name. Might want tat later but for now I am interested in the ports ex .A ( net ) Ideally I want to capture all the input net names (those with A,B,C,D etc) and the single output .Y ( net)

Eventually I want to store them into a hash where the output net is the key and the data is a ref to the array of inputs but for now I'm just trying to get all the input nets to be captured.

This is what I'm currently working with

open (FILE, "<maca") or die("Can not open $file");
  while (defined(my $cur_line = <FILE>)) {

    if ($cur_line =~ m/[A-Z].*?\.[A-C]\d* \( (.*?) \).*?;/mg) { 
      print "THIS gate $cur_line $1 $2 $3\n";  
      }
  }

I'm trying for this display

THIS gate NAND3_X1R_A9PP96CTUL_C16 SYN_INC_154 ( .A ( n1563 ) , .B ( U_PAUSEwcnt ) , .C ( n1640 ) , .Y ( n1467 ) ) ;

n1563 U_PAUSEwcnt n1640

But I get this. Actually I don't care about the first line just the 2nd. The first is for debugging. I thought the m would search multiple lines and the g would globally match the multi line string. What am I missing

THIS gate .B ( U_PAUSEwcnt ) , .C ( n1640 ) , .Y ( n1467 ) ) ;

n1640

If I understand you correctly you're looking for something like this:

while ($data =~ /(\w+)\s*\((.+?)\)\s*;/gm) {
  my $line = $1;
  my $vals = $2;
  while ($vals =~ /\.(\w+)\s*\(\s*(\w+)\s*\)/g) {
    print "$line .. $1: $2.\n"
  }
}

I called the variable $data as it has all lines - correct? I first split the lines, capturing the string between the ( .. ) , then pull out the key - value pairs. Looks like all names are alpha-numeric + "_" which is nicely captured per \\w .

Hope this helps?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM