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.
In reply to Re: positive regex for inverted match
by BrowserUk
in thread positive regex for inverted match
by kgimpel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |