无法创建未知的正则表达式:$ RE {some} {pattern}

时间:2019-03-28 11:53:57

标签: regex perl

我这里有一段代码,当我使用Regexp :: Common模块运行它时,会引发以下错误-“无法创建未知的正则表达式:$ RE {some} {pattern}”。

代码的目的是读取文件,搜索字符串并执行2个条件。第一个条件:如果某个引脚存在功能计时,则搜索字符串“ direction:output”,在下面找到连续的行,并删除包含“ max_transition:”的字符串,如果该功能计时不存在,则什么也不做。如果存在计时功能,并且搜索字符串为“ direction:input”,则不执行任何操作。这需要在文件中多次执行。当我使用if($ RE {some} {pattern}-> matches($ str))行时,代码将引发以上错误。请帮助我解决此错误。

#!/usr/bin/perl
use Data::Dumper;
use Regexp::Common;
use strict;
use warnings;
my $inputfile = $ARGV[0];
my $str = "direction : output";
open (INFILE,"<","$inputfile") || die "Can not open Input LIB File";
open (my $OPFILE,">","tmpdir/input_lib.lib") || die "Can not open Input Text File";
    sub do_something_with {
        my $string=shift;
        if( $RE{some}{pattern}->matches($str) )  {
             if ($string =~ m{timing +($RE{balanced}{-parens=>'()'} +$RE{balanced}{-parens=>'{}'})})  { # has a timing group
            $string =~ s{^.*max_transition.*\r?\n}{}m;
            }
        }
    }
        return $string;
    }
        local $/;
my $string=<INFILE>;
$string=~ s{pin +($RE{balanced}{-parens=>'()'} +$RE{balanced}{-parens=>'{}'})}{"pin ".do_something_with($1)}ge;
print $OPFILE "$string";
close INFILE;
close $OPFILE;

我的输入是:

cell (lib) {
    dont_use : true ;
    dont_touch : true ;
    pin ("A") {
      direction : input ;
      related_power_pin : VDD ;
      max_transition : 1 ;
    }  
    pin ("B")    {
      direction : output ;
      related_power_pin : VDD ;
      max_transition : 10;
    }              
    pin ("C") {
      direction : input ;
      related_power_pin : VDD ;
      max_transition : 1 ;
    }  
    pin ("D")    {
      direction : output ;
      max_transition : 10;
      related_power_pin : VDD ;
    }            

    pin ("E")    {
      direction : output ;
      clock : true ;
      max_transition : 20;
      related_power_pin : VDD ;
      related_ground_pin : VSS ;
      timing () {
        cell_fall (into_f1) {
          index_1("1,2,3,4,5") ;
          index_2("1,2,3,4,5") ;
          values("13, 13, 14, 16, 18",\
                 "13, 14, 15, 16, 19",\
                 "14, 15, 16, 17, 20",\
                 "15, 15, 16, 18, 20",\
                 "15, 16, 17, 18, 21") ;
        }  

    pin ("F")    {
      direction : input ;
      clock : true ;
      max_transition : 20;
      related_power_pin : VDD ;
      related_ground_pin : VSS ;
      timing () {
        cell_fall (into_f1) {
          index_1("1,2,3,4,5") ;
          index_2("1,2,3,4,5") ;
          values("13, 13, 14, 16, 18",\
                 "13, 14, 15, 16, 19",\
                 "14, 15, 16, 17, 20",\
                 "15, 15, 16, 18, 20",\
                 "15, 16, 17, 18, 21") ;
        }  
      }  
    }

我的预期输出是:

cell (lib) {
    dont_use : true ;
    dont_touch : true ;
    pin ("A") {
      direction : input ;
      related_power_pin : VDD ;
      max_transition : 1 ;
    }  
    pin ("B")    {
      direction : output ;
      related_power_pin : VDD ;
      max_transition : 10;
    }              
    pin ("C") {
      direction : input ;
      related_power_pin : VDD ;
      max_transition : 1 ;
    }  
    pin ("D")    {
      direction : output ;
      max_transition : 10;
      related_power_pin : VDD ;
    }            

    pin ("E")    {
      direction : output ;
      clock : true ;
      related_power_pin : VDD ;
      related_ground_pin : VSS ;
      timing () {
        cell_fall (into_f1) {
          index_1("1,2,3,4,5") ;
          index_2("1,2,3,4,5") ;
          values("13, 13, 14, 16, 18",\
                 "13, 14, 15, 16, 19",\
                 "14, 15, 16, 17, 20",\
                 "15, 15, 16, 18, 20",\
                 "15, 16, 17, 18, 21") ;
        }  

    pin ("F")    {
      direction : input ;
      clock : true ;
      max_transition : 20;
      related_power_pin : VDD ;
      related_ground_pin : VSS ;
      timing () {
        cell_fall (into_f1) {
          index_1("1,2,3,4,5") ;
          index_2("1,2,3,4,5") ;
          values("13, 13, 14, 16, 18",\
                 "13, 14, 15, 16, 19",\
                 "14, 15, 16, 17, 20",\
                 "15, 15, 16, 18, 20",\
                 "15, 16, 17, 18, 21") ;
        }  
      }  
    }

1 个答案:

答案 0 :(得分:3)

您已经从Regexp::Common documentation复制/粘贴了示例,但误解了它们的含义。

  

从%RE返回的模式是对象,所以不要写:

if ($str =~ /$RE{some}{pattern}/ ) {...}
     

您可以写:

if ( $RE{some}{pattern}->matches($str) ) {...}

这里的{some}{pattern}是您实际模式的占位符。我相信您想要的是$RE{balanced}{-parens=>'()'},您已经正确地使用了下一行。