Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: positive regex for inverted match

by BrowserUk (Patriarch)
on Feb 26, 2004 at 08:01 UTC ( [id://331933]=note: print w/replies, xml ) Need Help??


in reply to positive regex for inverted match

If the program that's calling the function is accepting a regex input by a user, requiring them to use '/^(?:(?!bar).)*$/s' when they want to find things that don't contain some word or phrase is not very friendly.

If the input is simple text, you might get away with passing "/^(?:(?!$their_input).)*$/s", thus wrapping their input in the appropriate construct if they specified 'NOT bar' or '!bar', or some flag to indicate that they want to invert the sense of the comparison. You'd have to strip the flag from the input before calling the function.

But if your going to have a flag they can use to indicate the inversion of the match, why not simply pass that flag through to the function and use it to determine the operator to use

sub do_selection{ my( $flag, $match ) = @_; if( $flag ) { do_stuff if $data !~ $match; else { do_stuff if $data =~ $flag; } } my( $flag, $regex) = get_input(); do_selection( $flag, $regex );

That's the way grep -v works, which is a pretty good precedent.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Timing (and a little luck) are everything!

Replies are listed 'Best First'.
Re: Re: positive regex for inverted match
by Crian (Curate) on Feb 26, 2004 at 09:11 UTC
    I think that is a good suggestion. But perhaps it would be a good idea to take the flag as the second parameter (and use it optional) like this:
    sub do_selection ($;$) { my $match = shift; my $flag = shift || 0; ...
    In this case you don't have to change lots of other calls of do_selection.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-25 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found