Re: negative regex without !~
by hv (Prior) on Jul 22, 2004 at 16:18 UTC
|
/^(?!.*$pattern)/;
If the string can include newlines there are a couple of ways to replace the '.' without changing the meaning of $pattern: /^(?!.*(?-s:$pattern))/s;
/^(?![\d\D]*$pattern)/;
Hugo | [reply] [Watch: Dir/Any] [d/l] [select] |
|
That is exactly what I was looking for. Thanks.
| [reply] [Watch: Dir/Any] |
Re: negative regex without !~
by dragonchild (Archbishop) on Jul 22, 2004 at 15:34 UTC
|
if ($string !~ /asdfasdf/) { ... }
is equivalent to
if (! ($string =~ /asdfasdf/) ) { ... }
or
unless ($string =~ /asdfasdf/) { ... }
If that doesn't suffice, a better problem description is required.
Update: Fixed precendence issue that merlyn correctly noted.
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose
I shouldn't have to say this, but any code, unless otherwise stated, is untested
| [reply] [Watch: Dir/Any] [d/l] |
|
if ($string !~ /asdfasdf/) { ... }
is equivalent to
if (! $string =~ /asdfasdf/) { ... }
Not really. The latter parses as
if ((! $string) =~ /asdfasdf/) {....}
That's why not was invented... because the precedence of "!" is often far too high for most operations.
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: negative regex without !~
by davido (Cardinal) on Jul 22, 2004 at 15:42 UTC
|
In Perl, the following examples are functionally equivilant, though they get to that equivilancy in different ways.
if ( $string !~ m/pattern/ ) { ..... }
And.....
if ( not $string =~ m/pattern/ ) { ...... }
And.....
unless ( $string =~ m/pattern/ ) { ...... }
Does your special language provide you with More Than One Way To Do It? ;)
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: negative regex without !~
by diotalevi (Canon) on Jul 22, 2004 at 17:08 UTC
|
/(?:$RegexGoesHere(?!)|(?=))/Ok, right. The previous construction was incorrect. I think using the experimental conditional test would work here since now the (?!) does not connect via backtracking to the (?=). /(?((?:$RegexGoesHere))(?!)|(?=))/
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] [d/l] |
Re: negative regex without !~
by Plankton (Vicar) on Jul 22, 2004 at 16:35 UTC
|
| [reply] [Watch: Dir/Any] |
Re: negative regex without !~
by Anonymous Monk on Jul 22, 2004 at 18:25 UTC
|
| [reply] [Watch: Dir/Any] [d/l] |
Re: negative regex without !~
by arden (Curate) on Jul 22, 2004 at 15:36 UTC
|
What piece of software are you dealing with that "cannot do negative matches"? I'm going to go out on a limb here and guess it isn't Perl, which leads to my second question: What does this have to do with Perl? Why are you asking at PerlMonks?
Again, if your "special" software doesn't allow for !~ then how are we to guess at if something else will succeed or fail within this "special" software without knowing what that software is?
- - arden. | [reply] [Watch: Dir/Any] |
|
I'm going to go out on a limb here and guess it isn't Perl
A questionable assumption. Define Perl.
One likely scenario is software compiled with the PCRE library. A growing number of applications do so these days. I know Apache will use it if it's there (although I don't know what for... maybe mod_rewrite these days?) and so will Postfix (and I'm a heavy user of it and very happy it's available).
A pure-Perl scenario I can envisage is a callback/library situation, where you get to construct an object or otherwise pass a qr/.../ where the design (admittedly somewhat brain-damaged) only admits positive matches. The not, ! and unless solutions in the other responses does one no good if you can't manoeuvre around the code that's doing the matching.
So please don't be so hasty in dismissing a person and telling them to go away. I agree the OP should have provided more information, but since it was posted here, maybe it has something to do with Perl?
- another intruder with the mooring of the heat of the Perl
| [reply] [Watch: Dir/Any] |
|
But "PCRE" isn't Perl, even in spite of the name. And it never can be. It can only be "Perl-like". So if someone has a question about the regex there, they should go to http://perl-like-monks.org, not here!
| [reply] [Watch: Dir/Any] |
|
There are many perl CGI and Tk programs that ask a user to enter regular expressions. If you are entering a regular expression in an entry box you typically cannot put '!~' on the front of it to negate it. I don't know if this is the kind of thing the original poster is talking about, but it seems like a very possible scenario
| [reply] [Watch: Dir/Any] |
|
File::Repl has a function that takes a qr (or a string that will be eval'ed as a regex), for instance. If I want to make a zip of a directory containing all files that are not ending in ".obj" and also do not contain the string "super-secret-gamma-ray-gun", I can't do that with File::Repl and I have to post-match.
| [reply] [Watch: Dir/Any] |
|
I'm fairly sure the $foo !~ m/../ syntax didn't exist in perl 5.0, but my impression to the original poster's question is that it really doesn't relate to perl itself of any version. I used to have to write a lot of backwards compatible code, so this was useful knowledge.... but who's still using 5.0 these days <grin>
| [reply] [Watch: Dir/Any] [d/l] |
|
use strict;
print 'Perl version is: ', $];
my @foo = qw/ fee fie fo fum big bag boz /;
foreach ( @foo ) {
if ( $_ !~ /f/ ) { print "Matched: $_ !~ /f/ " ; }
else { print "Failed: $_ !~ /f/ " ; }
}
Output:
Perl version is: 5.00503
Failed: fee !~ /f/
Failed: fie !~ /f/
Failed: fo !~ /f/
Failed: fum !~ /f/
Matched: big !~ /f/
Matched: bag !~ /f/
Matched: boz !~ /f/
-------------------------------------
Nothing is too wonderful to be true
-- Michael Faraday
| [reply] [Watch: Dir/Any] [d/l] |
|
|
No, I recall using !~ even back in Perl 4.
| [reply] [Watch: Dir/Any] [d/l] |