Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Hazardous characters filter

by Kenosis (Priest)
on Mar 24, 2014 at 19:33 UTC ( #1079585=note: print w/replies, xml ) Need Help??

in reply to Hazardous characters filter

Perhaps the following, which uses a negative lookhead and a negative lookbehind, will be helpful:

use strict; use warnings; my $test = "this is not OK < but this is ok <url>"; print rep($test); sub rep { $_[0] or return $_[0]; $_[0] =~ s/<(?!url)|(?<!url)>//g; return $_[0]; }


this is not OK but this is ok <url>

Your sub returns '' if $_[0] evaluates to false. This means that 0 and !defined $_[0] would convert to '', and perhaps that's your intent. In these cases, the above just returns what was sent (if anything), so you may need to adjust it.

Replies are listed 'Best First'.
Re^2: Hazardous characters filter
by no_slogan (Deacon) on Mar 25, 2014 at 14:59 UTC
    Lookahead/lookbehind is a good suggestion. The reason I didn't suggest it is that I expected the OP to respond that the < > should surround an actual url, not the literal string "url".

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2023-02-06 00:46 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (33 votes). Check out past polls.