Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Replace newlines only if not inside braces

by smls (Friar)
on Feb 11, 2013 at 13:54 UTC ( #1018145=note: print w/ replies, xml ) Need Help??


in reply to Replace newlines only if not inside braces

Replacing a target pattern everywhere except inside specific chunks, can be achieved with a regex of the following form:

s/((?:CHUNK_TO_BE_EXCLUDED|.)*?)TARGET/$1REPLACEMENT/gs

In your example, TARGET would be \n and REPLACEMENT would be \n<br>. The CHUNK_TO_BE_EXCLUDED pattern would have to match a whole block wrapped in double braces. You can use {{.*?}}\n, unless brackets can be nested and you need to guarantee that you match properly balanced pairs, in which case you can find a howto for constructing the pattern you need in perlfaq6.


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:35 UTC
    Works! =)

    But it took me some time to understand why all edgecases are covered, though I'm pretty sure that I already saw this technique before.

    Cheers Rolf

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2014-07-14 04:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (254 votes), past polls