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

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).

s/\<\/div\>/,/\<\/body\>/
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

--Chris

Yes. What say about me, is true.


Comment on Re^2: Perl RE; how to capture, and replace based on a block?
Download Code
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\>};
Re^3: Perl RE; how to capture, and replace based on a block?
by Laurent_R (Vicar) 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.

      --Chris

      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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2014-07-13 03:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (245 votes), past polls