Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Regexp: Match anything except a certain word

by ikegami (Pope)
on Apr 21, 2006 at 17:06 UTC ( #544947=note: print w/replies, xml ) Need Help??

in reply to Regexp: Match anything except a certain word

$test =~ s/(<a\s(?:(?!target=)(?!>).)*)(?=>)/$1 target="_blank"/sgi;


Replies are listed 'Best First'.
Re^2: Regexp: Match anything except a certain word
by fraktalisman (Hermit) on Apr 21, 2006 at 17:19 UTC

    Thanks! This looks good, although it does not check for http and so it also put the target to local links, but I should be able to put the pieces together now.

      Oops, I read the problem too fast. Fix:
      $test =~ s{ ( <a\s (?:(?!target=|>).)* href="http:// (?:(?!target=|>).)* ) (?=>) }{$1 target="_blank"}xsgi;

      Alternatively, the following *might* be faster, especially considering href is usually the first attribute:

      $test =~ s{ ( <a\s (?:(?!href=|target=|>).)* href="http:// (?:(?!target=|>).)* ) (?=>) }{$1 target="_blank"}xsgi;


      Update: Collapsed (?!..re1..)(?!..re2..) into (?!..re1..|..re2..)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://544947]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2017-02-20 06:44 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (294 votes). Check out past polls.