#!/usr/bin/env perl use Data::Dumper; use Regexp::Common; use 5.12.0; my $library_group_re=qr{ library\ * # "library" followed by optional spaces ( # $1 capture (?$RE{balanced}{-parens=>'()'}) # balanced parenthesis \ * # followed by optional spaces (?$RE{balanced}{-parens=>'{}'}) # balanced braces ) # end $1 capture }x; my $operating_conditions_group_re=qr{ operating_conditions\ * # "operating_conditions" followed by optional spaces ( # $1 capture (?$RE{balanced}{-parens=>'()'}) # balanced parenthesis \ * # followed by optional spaces (?$RE{balanced}{-parens=>'{}'}) # balanced braces ) # end $1 capture }x; sub do_something_with_an_operating_conditions { #warn Data::Dumper->Dump([\@_],[qw(*_)]),' '; my (%captured)=@_; if (!exists $captured{paren} || ! exists $captured{braces}) { die "Should NOT happen!"; }; my $return='operating_conditions '; # process the stuff within the parenthesis - $return.="$captured{paren} "; # process the stuff within the braces $return.="$captured{braces}"; # The additional line. $return.="\n default_operating_conditions : ".substr($captured{paren},1,-1)."\n"; return $return; }; sub do_something_with_a_library { #warn Data::Dumper->Dump([\@_],[qw(*_)]),' '; my (%captured)=@_; if (!exists $captured{paren} || ! exists $captured{braces}) { die "Should NOT happen!"; }; my $return='library '; # process the stuff within the parenthesis - $return.="$captured{paren}"; # process the stuff within the braces - operating conditions { (my $string=$captured{braces})=~ s{$operating_conditions_group_re}{do_something_with_an_operating_conditions(%+)}gex; $return.=" $string"; }; return $return; }; my $string; { # Because we want all of the data as a single string local $/; # Deal with string, which will be the entire file, replacing each of library groups ($string=) =~ s{$library_group_re}{do_something_with_a_library(%+)}gex; } print $string; __END__ library(and_gate) { delay_model : table_lookup ; date : "Fri Mar 15 03:44:39 " ; time_unit : 1ms ; voltage_unit : 1V ; current_unit : 1A ; operating_conditions ("AB0.5v45c") { process : 1 ; temperature : 45 ; voltage : 0.5 ; } input_voltage(default) { vi : 0 ; vh : 0.5 ; vim : 0 ; vin : 0.5 ; } }