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

Re^2: Replace newlines only if not inside braces

by LanX (Chancellor)
on Feb 11, 2013 at 14:52 UTC ( #1018155=note: print w/replies, xml ) Need Help??

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

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

Replies are listed 'Best First'.
Re^3: Replace newlines only if not inside braces
by trizen (Hermit) on Feb 11, 2013 at 17:00 UTC
    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

        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).
Re^3: Replace newlines only if not inside braces
by 7stud (Deacon) on Feb 11, 2013 at 16:54 UTC


    Not quite.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1018155]
[TCLion]: at least that's the plan
[1nickt]: Corion this looks good, from the pod: "there are 9000+ variations that are detected correctly in the test files (see t/data/* for most of them). If you can think of any that I do not cover, please let me know."
[TCLion]: some moron put the date like this : Mon Feb 20 09:31:30 2017
[Corion]: 1nickt: Yes, the module sounds promising indeed
[1nickt]: "putting the date in correct order" how?
[Corion]: TCLion: Whee ;)
[TCLion]: need to put like this : 2017-02-20 09:30:53
[1nickt]: That's why I asked if you are using DateTime. It has a large number of supporting modules (the author likes the term 'eco-system') so if you are already creating a DateTime obj from your dates, this module would read in the mnoron-formatted 1s seamlessly
[TCLion]: when I put the date together it looks like : 2017-Feb-24 (month is the problem)
[1nickt]: good luck, then.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2017-03-23 14:56 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (288 votes). Check out past polls.