Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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

by Laurent_R (Abbot)
on Dec 18, 2013 at 07:46 UTC ( #1067604=note: print w/replies, xml ) Need Help??

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

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)?
  • Comment on Re^3: Perl RE; how to capture, and replace based on a block?

Replies are listed 'Best First'.
Re^4: Perl RE; how to capture, and replace based on a block?
by taint (Chaplain) on Dec 18, 2013 at 08:02 UTC
    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.

        Thanks Laurent_R for the help.

        Your example helps better explain why I got the impression Perl was not greedy, the way sed is. Most all the examples I read in the perldocs, used .*?. But my experiments, and re-training myself with Perl RE's. Seems to show, once I get out of the sed habits. Perl will be a lot more flexible, and powerful. I should have started this a l-o-o-n-g time ago. :P

        Thanks again, Laurent_R. I really appreciate it.


        Yes. What say about me, is true.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1067604]
[shmem]: result := the discarded result (what's leftmost of the statement)
[shmem]: push returns the number of elements in @foo, the assignment returns all the elements in the (renewed) @foo
[Lady_Aleena]: Sanity check please? push @line, ref($list_addition ) eq 'ARRAY' ? @$list_addition : $list_addiction; # so I don't have the change existing uses of the sub.
[shmem]: $list_addiction should be $list_addition
Discipulus loose the tozzetto party!
[shmem]: .oO( addition of cookies for addiction to cookies )
[Lady_Aleena]: Other than my typos shmem. 8)
[shmem]: otherwise fine
[Discipulus]: tozzetti & vinsanto
[Lady_Aleena]: The whole line is push @line, ref($list_addition ) ? @$list_addition : $list_addition if $list_addition;

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2017-04-27 11:42 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (503 votes). Check out past polls.