Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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]; }

Output:

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2021-03-02 09:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favorite kind of desktop background is:











    Results (42 votes). Check out past polls.

    Notices?