Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Replace newlines only if not inside braces

by LanX (Bishop)
on Feb 11, 2013 at 13:25 UTC ( #1018136=note: print w/replies, xml ) Need Help??

in reply to Replace newlines only if not inside braces

As long as the braces groups are not nested you could do it by separating blocks in a split and handling them differently.
use Data::Dump; my $data = q| foo bar {{ alpha beta }} baz |; @splits = split /({{.*?}})/s, $data; dd \@splits; my $result=""; while (my $block = shift @splits) { $block =~ s/\n/<br>\n/gs; $result .= $block; $result .= shift @splits if @splits; } print $result;

["\nfoo\nbar\n", "{{\nalpha\nbeta\n}}", "\nbaz\n"] <br> foo<br> bar<br> {{ alpha beta }}<br> baz<br>

I refrain from trying a complicated and potentially unmaintainable one-line regex solution.

Some come to mind¹, but I don't see the necessity if there are no other restrictions (like lack of memory) involved.

Cheers Rolf

¹) like
  • looping with while (/({{.*?}})/gs) (and \G and pos)
  • using /e to do substitution within substitutions
  • complicated look-ahead and look-behind assertion
  • using \K somehow to restrict replacement

Replies are listed 'Best First'.
Re^2: Replace newlines only if not inside braces
by jbryan (Acolyte) on Feb 11, 2013 at 21:24 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1018136]
[LanX]: or other language?
[moritz]: LanX because of usability issues, and the lack of evolvement
[holli]: lol to the robust charme or to the data transfer?
[holli]: it works fine with Lynx though :)
[LanX]: it's easier to patch then to migrate to a PHP board, there are many semantics like linking , authorisation, templates ,...
[LanX]: I've only seen 30% of the semi professional documentation
[holli]: i was just thinking of the bare content.
[LanX]: Plenty of "templates" realised with CGI-HTML-function
[LanX]: well if you want you can fetch all XML and translate it to another board markup
[moritz]: at this time, a migration could consist of only migrating the rendered HTML of nodes

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2017-11-20 19:29 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (292 votes). Check out past polls.