Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: Replace newlines only if not inside braces

by trizen (Friar)
on Feb 11, 2013 at 17:00 UTC ( #1018184=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Replace newlines only if not inside braces
in thread Replace newlines only if not inside braces

No, there is no difference. I just thought it would be a little bit more efficient. I thought that, if the \K is outside, the $& variable is being cleaned up for every substitution, which is not really necessary. It should be cleaned only when something on the left side has been matched. Anyway, it is more readable in your way, and does, basically, the same thing. :)

Alternatively, to work with strings that contain {{...}} groups, which are not followed by a newline, this code should do it:

$data =~ s<(?:{{.*?}}|[^\n])*\K\n>{<br>\n}gs; print $data;


Comment on Re^3: Replace newlines only if not inside braces
Select or Download Code
Re^4: Replace newlines only if not inside braces
by LanX (Canon) on Feb 11, 2013 at 20:15 UTC
    $data =~ s<(?:{{.*?}}|[^\n])*\K\n>{<br>\n}gs;

    I think (didn't test it) that the * is two greedy, better *?.

    Cheers Rolf

      Nope. It doesn't matter. The group doesn't match a single newline, so it can be greedy. It will try to match either the brackets block or any other character which is not a newline. If it succeeds, then tries again to match the block or a non-newline character. If it fails, it will match the newline character (\n) and makes the replace.

      Using the non-greedy quantifier, will do the same thing, but a little bit slower because it needs, unnecessary, to look ahead after each repetition (theoretically).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2014-08-30 23:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls