Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Hazardous characters filter

by Kenosis (Priest)
on Mar 24, 2014 at 19:33 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1079585]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-16 06:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found