Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Bug in script, regex help req extreme urgent

by Athanasius (Chancellor)
on Mar 10, 2013 at 04:27 UTC ( #1022643=note: print w/replies, xml ) Need Help??

in reply to Bug in script, regex help req extreme urgent

Hello sid.verycool, and welcome to the Monastery!

Please note that when a regular expression is assigned to a string prior to its use in a match, the string should be quoted using Perl’s qr// operator. That is, a line such as:

my $match = "(?<=module )$ARGV[2].*?([\\(;])";

is better written:

my $match = qr/(?<=module )$ARGV[2].*?([\\(;])/;

See Regexp Quote Like Operators in perlop for the advantages of using this form of quoting.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Bug in script, regex help req extreme urgent
by sid.verycool (Novice) on Mar 10, 2013 at 05:18 UTC
    thanks Althanasius, this monastry is indeed a gr8 place to come to (with the number of patient and helpful people i have come across in only 1 day) Actually im not sure what $ARGV[2].*?([\\(;])/ is doing, $ARGV2 is the argument i know suppose i passed OLD i,e, $ARGV2=OLD, now can any1 explain the intent of (?<=module )$ARGV[2].*?([\\(;]) i know . means any character * 0 or more occurrences,

      Tip #9 from the Basic debugging checklist:

      Demystify regular expressions by installing and using the CPAN module YAPE::Regex::Explain


      #! perl use strict; use warnings; use YAPE::Regex::Explain; my $re = qr/(?<=module )$ARGV[2].*?([\\(;])/; print YAPE::Regex::Explain->new($re)->explain();


      15:40 >perl FILE OLD NEW The regular expression: (?-imsx:(?<=module )NEW.*?([\\(;])) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- (?<= look behind to see if there is: ---------------------------------------------------------------------- module 'module ' ---------------------------------------------------------------------- ) end of look-behind ---------------------------------------------------------------------- NEW 'NEW' ---------------------------------------------------------------------- .*? any character except \n (0 or more times (matching the least amount possible)) ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- [\\(;] any character of: '\\', '(', ';' ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- 15:40 >

      See also perlretut and perlre.

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        ohk... i didnt knew about this feature that explains like this. Another important point i wish to make, I want to ignore // lines. Suppose somebody writes something like // module OLD(Y, A, B); would my $string not match and make script behavior wrong?? thats why i was trying to give ^module (WHICH MAKE SURE THAT ITS NOT A COMMENT). WHITESPACE AND TABS ARE OK BUT NO // BEFORE matching expression
        It solves my issue but fails in the scenario when i have module this is in between OLD(A, B, Y); , i even want my script to change it to, module this is in between NEW(A, B, Y); hence i tried doing this (?<=module .*)$ARGV2.*?(\\(;)"; which doesnt work and shell says Variable length lookbehind not implemented in regex; marked by <-- HERE in m/(?<=module .*)NEW.*?(\(;) <-- HERE / at line 23.. How can i implement this?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1022643]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (1)
As of 2018-01-18 04:07 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (206 votes). Check out past polls.