Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Replace newlines only if not inside braces

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


in reply to Replace newlines only if not inside braces

You can match and discard something that you don't want to replace. For example, match the group {{...}} and use the \K to replace only the right side, keeping the left side of \K as it is.

Code:

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


Comment on Re: Replace newlines only if not inside braces
Select or Download Code
Re^2: Replace newlines only if not inside braces
by LanX (Canon) on Feb 11, 2013 at 14:52 UTC
    Works as long as }} is immediately followed by a \newline!

    Is there a reason why you've put the \K within the group?

    I suppose this does the same and is better readable!

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

    Cheers Rolf

    1) see or clause in Re: Replace newlines only if not inside braces for a work around

      Works

      Not quite.
      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;
        $data =~ s<(?:{{.*?}}|[^\n])*\K\n>{<br>\n}gs;

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

        Cheers Rolf

Re^2: Replace newlines only if not inside braces
by 7stud (Deacon) on Feb 11, 2013 at 16:33 UTC
    Did that s/// win an obfuscation contest somewhere?
    use warnings; use strict; use 5.012; my $data = <<'END_OF_TEXT'; foo bar {{ alpha beta }} baz END_OF_TEXT $data =~ s/ (?: #Non-capturing group {{.*?}} #Text enclosed by double braces \K #Exclude what's to the left of \K from match )? #Match whole group 0 or 1 time \n /\n<br>/gxms; say $data; --output:-- foo <br>bar <br>{{ alpha beta }} <br>baz <br>

    It would make more sense to put the newlines after the breaks if you were trying to pretty print some html.

      Did that s/// win an obfuscation contest somewhere?
      so what's your contribution?
      It would make more sense to put the newlines after the breaks if you were trying to pretty print some html.
      minor problems of minor minds...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (16)
As of 2014-12-18 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (48 votes), past polls