Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^2: Perl RE; how to capture, and replace based on a block?

by taint (Chaplain)
on Dec 18, 2013 at 07:11 UTC ( #1067597=note: print w/replies, xml ) Need Help??

in reply to Re: Perl RE; how to capture, and replace based on a block?
in thread Perl RE; how to capture, and replace based on a block?

Thank you educated_foo.

" Keep adding "VAR," between the parens in #2 as long as Perl complains about 'Global symbol "VAR"...' Someone should probably write an Acme:: module to do this automatically."

I'll be glad to. Just as soon as I figure this all out. :)

My biggest hangup, I think, is that I'm quite comfortable with sed. But sed is "greedy" by default, and while Perl RE can be. It's not, by default, and that's what I need here (not greedy).

will match my pattern in sed. But it will match from the first </div> till the first </body>. Which is too much.

Thanks again for the response, educated_foo


Yes. What say about me, is true.

Replies are listed 'Best First'.
Re^3: Perl RE; how to capture, and replace based on a block?
by Laurent_R (Canon) on Dec 18, 2013 at 07:46 UTC
    By default, Perl RE are greedy. Have you considered the possibility that the end of line might be more than \n (if the file is coming from Windows, for example)?
      Greetings, Laurent_R, and thanks for the reply.

      Oh yes. I'm keen on the \n v \r v \n\r thing, and you're absolutely correct. Except, in my case, I'm on a *NIX box, and I've written the files myself. So I know they're utf-8 (no BOM), with newlines, no "hard" returns. :)

      Maybe it's just the examples I was reading (perlrequick, perlretut, and perlfaq6) but I got the impression that Perl RE wasn't greedy. More Perl RE reading, I guess.

      Thanks again, for the response Laurent_R.


      Yes. What say about me, is true.

        Hi Chris

        just a quick example on greedy and non greedy quantifiers, under the Perl debugger:

        DB<1> $string = "foo bar foo bar foo"; DB<2> x $string =~ /(fo.*ar)/ 0 'foo bar foo bar' DB<3> x $string =~ /(fo.*?ar)/ 0 'foo bar' DB<4>
        As you can see the default "*" quantifier is greedy, so that the regex tries to match as much as possible. Using the modified "*?" quantifier, the regex becomes non greedy. You will see the same type of result if you try the "+" and the "+?" quantifiers.

Re^3: Perl RE; how to capture, and replace based on a block?
by Anonymous Monk on Dec 18, 2013 at 07:29 UTC

    ... sed ...

    here is my test program

    use re 'debug'; $_ = q{</div> </body>}; print 'does it match ', int m{\<\/div\>\n\<\/body\>};

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1067597]
[mz2255]: I'm trying to post a question to "seekers of perl wisdom" but always getting a permission denied when I want to submit. There are no links in my post...
[ambrus]: again? someone complained about this just a few days ago (although eventually they could post)
[choroba]: Can you post the question in mz2255's scratchpad?
[ambrus]: Corion, if you're here, can you check the spam filter logs to see what's triggering this time?
[ambrus]: Petroza had trouble posting yesterday, but has posted Issues Fetching URL with a variable token since.
[mz2255]: Yes, just edited the scratchpad. Not sure if I'm doing something wrong, it's my first time.

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2017-10-19 15:25 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (255 votes). Check out past polls.