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

In reply to Re: Using regex with a variable by Marshall
in thread Using regex with a variable by Praveen Dharmaraj

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.