Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Negating Regexes: Tips, Tools, And Tricks Of The Trade

by imp (Priest)
on Dec 07, 2006 at 14:34 UTC ( #588329=note: print w/replies, xml ) Need Help??


in reply to Negating Regexes: Tips, Tools, And Tricks Of The Trade

I was experimenting with this and came up with the following .
use strict; use warnings; my $pattern = qr/abc/; my $negative = qr< (??{ /$pattern/ ? qr/\A$/ : qr//; }) >x; my $text = 'abcdef'; if ($text =~ /$negative/) { print "matched $text\n"; }
This strategy uses (??{ code }) to evaluate the current string against the regex you wish to negate.
If the pattern matches it returns a regex that matches an empty string.
Otherwise it returns a regex that matching anything.

I'm not sure how safe this strategy is though. Can someone who knows more about perl's regex engine comment on whether the above is appropriate?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://588329]
help
Chatterbox?
[prathap keerthipati]: how to update perl in unix
[hippo]: yum update perl
[hippo]: Other package managers are available
LanX wouldn't update system Perl!
[Discipulus]: prathap keerthipati might be it is better to install an alternative Perl instead and do not touch the system one
[LanX]: see perlbrew for alternative Perl installations
[marto]: unless you know exactly what you're doing an often saner option is to simply build another Perl rather than replace the system one

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (16)
As of 2017-03-23 10:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (285 votes). Check out past polls.