Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Using regex with a variable

by Marshall (Canon)
on Mar 17, 2016 at 02:42 UTC ( [id://1158037]=note: print w/replies, xml ) Need Help??


in reply to Using regex with a variable

As many Monks have said, it is not possible for us to provide excellent help if you don't tell us what you are trying to accomplish. Dumb your example down and remove any proprietary stuff from it. But we need to know what the code below the if statements are doing.

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> } } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1158037]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-29 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found