Re: regular expressions with !
by Tomte (Priest) on Sep 14, 2006 at 14:34 UTC
|
You're a victim of operator precedence - the ! has higher precedence than =~ - so your expression evaluates as if ((!$test_string)=~/$regex/), which is false.
Either use !~, as already suggested, or use parenthesis around the whole match if (!($test_string =~/$regex/)).
You do not need to use the /e modifier!
regards,
tomte
An intellectual is someone whose mind watches itself. -- Albert Camus
| [reply] [d/l] [select] |
|
Hi,
thanks Tomte++! The only explanation that helped me understnad the problem right away!
Regards,
svenXY
| [reply] |
Re: regular expressions with !
by zigdon (Deacon) on Sep 14, 2006 at 14:32 UTC
|
As others mentioned, you need to use either 'not' or '()' - since '!' has a higher order of precedence than '=~'. From perlop:
Operator Precedence and Associativity
...
right ! ~ \ and unary + and -
left =~ !~
...
right not
| [reply] [d/l] |
Re: regular expressions with !
by ozone (Friar) on Sep 14, 2006 at 14:19 UTC
|
try using 'not' instead of '!', like so:
...
if(not $test_string =~ /$regex/) {
...
}
...
It's basically a question of operator precedence.
To make your intentions clearer, you should probably use the '!~' operator:
...
if($test_string !~ /$regex/) {
...
}
...
| [reply] [d/l] [select] |
|
...
if ($test_string !~ /$regex/) {
...
}
...
Perl operators and their precedence | [reply] [d/l] |
|
| [reply] [d/l] |
Re: regular expressions with !
by svenXY (Deacon) on Sep 14, 2006 at 14:26 UTC
|
Hi,
yes, 'not' works better:
#!/usr/bin/perl
use strict;
my $regex = 'foo';
my $test_string= 'bar';
print 'no match 1',"\n" if not $test_string =~ /$regex/;
print 'no match 2 ',"\n" if ! $test_string =~ /$regex/;
print 'no match 3', "\n" unless $test_string =~ /$regex/;
returns:
no match 1
no match 3
Regards,
svenXY | [reply] [d/l] [select] |
Re: regular expressions with !
by explorer (Chaplain) on Sep 14, 2006 at 14:32 UTC
|
If you want "no match", use it:
if ( $test_string !~ /$regex/) {
unless ( $test_string =~ /$regex/) {
| [reply] [d/l] [select] |
|
Or even unless ( ! ( $test_string !~ /$regex/ ) ) {.
Once you've taken the first step down the unless road, madness ensues. Don't use unless; it makes it too easy to misunderstand code.
| [reply] [d/l] [select] |
|
Don't use unless; it makes it too easy to misunderstand code.
I dislike overgeneralizations like this, personally. The following code is perfectly clear:
do_something() unless $big_honkin_error;
I'm not saying that you can't avoid unless and make the code still clear, of course:
do_something() if not $big_honkin_error:
but the unless version is just fine.
I wouldn't use unless at the beginning of a condition, however. But that's related to putting the important parts at the beginning of the statement as per perlstyle recommendations. Specifically:
Just because you *CAN* do something a particular way doesn't mean that you *SHOULD* do it that way. Perl is designed to give you several ways to do anything, so consider picking the most readable one. For instance
PrivoxyWindowOpen(FOO,$foo) || die "Can't open $foo: $!";
is better than
die "Can't open $foo: $!" unless open(FOO,$foo);
because the second way hides the main point of the statement in a modifier.
| [reply] [d/l] [select] |