in reply to Using regex with a variable
I probably made some mistakes below, but central to the OP question is "what do you want to accomplish?".
A simple optimization would be to run all 5 regexes on each line, save the results and use those results in the "if" statements. For example, regex 4 is run repeatedly for each and every "if" statement. my $ends_in_semicolon is a simple scalar that could be tested very quickly.
The value of $ur is "rba_Reset_Request". foreach my $ur (@strings_to_be_matched) { $reg1 = qr/\=/i; #line has a single '=' #/i makes no difference, delete +that! #there is no "capital ="! $reg2 = qr/\S+\=\S+/i; #line has a single "=" with some +thing #to the left and something to th +e right $reg3 = qr/extern.+\b$ur\b\s*/i; #line has extern rba_Reset_Reque +st $reg4 = qr/;$/i; #line ends in ";" # in C code you are not allowing for t +he #idea that whitespace may be after the + ";" # /;\s*$/; $reg5 = qr/.+\b$ur\b\s*/i; #line has rba_Reset_Request #could just be: #/\b$ur\b/; ? foreach my $line (@contents_of_file) { if(($ln =~ $reg3 and $ln =~ $reg4)){ <some code> # line contains: extern rba_Reset_Request # and ends in a ; } if(($ln =~ $reg5 and $ln=~ $reg4 and ($ln !~ $reg1 or $ln =~ $reg +2)) { # line contains rba_Reset_Request, ends in a ";", has an "=" sign +. <some code> } if(($ln =~ $reg3 and $ln !~ $reg4)){ # line contains extern rba_Reset_Request and doesn't end in ; <some code> } if(($ln =~ $reg5 and $ln !~ $reg4 and $ln !~ $reg1)) #line contains rba_Reset_Request doesn't end in ; or # have an = sign { <some code> } } }
|
|---|