in reply to Re^2: Using regex with a variable
in thread Using regex with a variable

++ for reformatting your code..

Now i see that $ur is declared after the regexes.. what your code is supposed to do? if you just put qr in the meanwhile $ur is uundefined and probably is not ywhat you want.. are you using strict and warnings? probably we need more informations about your intention and your possible data inputs.

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^4: Using regex with a variable
by Praveen Dharmaraj (Novice) on Mar 14, 2016 at 09:27 UTC
    Sorry for the misunderstanding. I wasn't assigning $ur with a value in code. The question is updated now. The value of $ur remains constant throughout the looping of lines. I cannot exactly paste all the code here. I am not allowed to. And yes, I am using "use Strict", "use Warnings".
      You can always change your sensible data to something else and the regex accordingly and post your intentions and some simple input.

      Whitout this it is impossible to guess what you are triyng to achieve.

      Probably a single regex can do the job you need. Here around are very good regexer (not me).

      Do not modify your original post without putting some note on changes: replies can have no sense after the modification of the original post.

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

        Hello,

        First of all, sorry for providing insufficient information about my problem. I understand now, that I need to explain my problem clearly for me to get some help.

        OK. Here goes..

        I am trying to find a few strings (stored in array) from about 50,000 lines of code in a file. After finding, I'm doing some changes to the matched strings and writing it to another file. To accomplish this task, I'm assigning 5 regexes to five variables.

        foreach my $ur (@strings_to_be_matched) { $reg1 = qr/\=/i; $reg2 = qr/\S+\=\S+/i; $reg3 = qr/extern.+\b$ur\b\s*/i; $reg4 = qr/;$/i; $reg5 = qr/.+\b$ur\b\s*/i; foreach my $line (@contents_of_file) { if(($ln =~ $reg3 and $ln =~ $reg4)){ <some code> } if(($ln =~ $reg5 and $ln=~ $reg4 and ($ln !~ $reg1 or $ln =~ $reg +2)) { <some code> } if(($ln =~ $reg3 and $ln !~ $reg4)){ <some code> } if(($ln =~ $reg5 and $ln !~ $reg4 and $ln !~ $reg1)) { <some code> } } }

        I'm using "qr" to pre-compile my regexes, so that I can reduce the time taken. I feel $reg5 is causing the problem here. The if conditions which involve $reg5 take longer time to match.

        if (($ln =~ $reg5 and $ln=~ $reg4 and ($ln !~ $reg1 or $ln =~ $reg2))) { <some code here...> } if (($ln =~ $reg5 and $ln !~ $reg4 and $ln !~ $reg1)) { <some code here...> }

        Another point to be noted is that, there are a few lines in the file which have a length of about ~12000. These lines take 4 seconds each to be processed.I want to reduce the time taken as few seconds taken to process each line are adding up to ~30 minutes. This is not acceptable at all in my case.

Re^4: Using regex with a variable
by Praveen Dharmaraj (Novice) on Mar 14, 2016 at 09:31 UTC
    Sorry for the misunderstanding. Im not assigning $ur with a value in code. It remains constant through out the looping process. And, I cannot exactly paste my code in here, as Im not allowed to. And yes, Im using "use strict" and "use warnings".

      Reduce the problematic code to a short stand alone example that shows the problem. See I know what I mean. Why don't you? for tricks to include sample data. If you need a lot of data, get the test script to generate it.

      We don't need to see your sensitive code or data, but we can't help much with hand wavy descriptions of the problem. We need a good representation of the structure of the code to be able to give good advice.

      Premature optimization is the root of all job security