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

Re: Advanced replace function

by kyle (Abbot)
on Dec 04, 2009 at 19:08 UTC ( #811144=note: print w/replies, xml ) Need Help??


in reply to Advanced replace function

You may be looking for the /g option to s///:

my $str="abc t[poop1] def [doop2] [woop3] xyz"; $str =~ s/\[.*?\]/~~/g; print "$str\n"; __END__ abc t~~ def ~~ ~~ xyz

Replies are listed 'Best First'.
Re^2: Advanced replace function
by ikegami (Pope) on Dec 04, 2009 at 19:49 UTC
    There was nothing about newlines being disallowed in the brackets. The following is faster and more likely to match correctly:
    s/\[.*?\]/~~/sg

    But that's risky, especially if you use it a larger pattern. The /.*?/ could match a "[" or a "]". That's obviously undesirable. The following pattern avoids that problem:

    s/\[[^\[\]]*\]/~~/g;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://811144]
help
Chatterbox?
[zentara]: hello jedikaiti what's up?
[erix]: apod is up !
[zentara]: that would make great screen wallpaper
jedikaiti is glad to see the conditioning persists...
[jedikaiti]: And yes, I have saved down a copy and added it to my walpaper rotation

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2017-08-18 15:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (303 votes). Check out past polls.

    Notices?