Can't Negate Regex /^8([0876])\1\d+$/ 
Author Message
 Can't Negate Regex /^8([0876])\1\d+$/

I am trying to negate the following regex which I am using
to filter toll free numbers. The regex seems to work fine as
is but when I try to negated it won't work.

$regex =~ /^8([0876])\1\d+$/;

even though I have tried many different combinations like

/^[^8([0876])\1]\d+$/
/^[^8([0876])]\1\d+$/

I can't get it to work properly. Any help is welcome.

--------------------------------------------------------------------------------
Victor Araujo



Tue, 27 Jan 2004 11:36:40 GMT  
 Can't Negate Regex /^8([0876])\1\d+$/


Wed, 18 Jun 1902 08:00:00 GMT  
 Can't Negate Regex /^8([0876])\1\d+$/

Quote:

> I am trying to negate the following regex which I am using
> to filter toll free numbers. The regex seems to work fine as
> is but when I try to negated it won't work.
> $regex =~ /^8([0876])\1\d+$/;
> even though I have tried many different combinations like
> /^[^8([0876])\1]\d+$/
> /^[^8([0876])]\1\d+$/
> I can't get it to work properly. Any help is welcome.

http://www.perl.com/CPAN-local/doc/FAQs/FAQ/PerlFAQ.html#perlfaq6_Reg...

Godzilla!



Tue, 27 Jan 2004 11:50:17 GMT  
 Can't Negate Regex /^8([0876])\1\d+$/


Wed, 18 Jun 1902 08:00:00 GMT  
 Can't Negate Regex /^8([0876])\1\d+$/

Quote:

> I am trying to negate the following regex which I am using
> to filter toll free numbers. The regex seems to work fine as
> is but when I try to negated it won't work.
> $regex =~ /^8([0876])\1\d+$/;

Just change the =~ to !~ (note that this works because your regex is
anchored at both ends and therefore covers the entire string you're trying
to match.  You have to be careful when inverting the sense of the match
operator if your regex can match only a portion of the target string).

Quote:
> even though I have tried many different combinations like
> /^[^8([0876])\1]\d+$/
> /^[^8([0876])]\1\d+$/

You can't use a character class to match a multi-character pattern; I
suggest you (re)read perlre.


Tue, 27 Jan 2004 11:57:34 GMT  
 Can't Negate Regex /^8([0876])\1\d+$/

Quote:

> I am trying to negate the following regex which I am using
> to filter toll free numbers. The regex seems to work fine as
> is but when I try to negated it won't work.

> $regex =~ /^8([0876])\1\d+$/;

> even though I have tried many different combinations like

> /^[^8([0876])\1]\d+$/
> /^[^8([0876])]\1\d+$/

> I can't get it to work properly. Any help is welcome.

$regex =~ /^(?!8([0876])\1)\d+$/

John
--
use Perl;
program
fulfillment



Tue, 27 Jan 2004 12:07:38 GMT  
 Can't Negate Regex /^8([0876])\1\d+$/

Quote:

>I am trying to negate the following regex which I am using
>to filter toll free numbers. The regex seems to work fine as
>is but when I try to negated it won't work.

>$regex =~ /^8([0876])\1\d+$/;

Do note that in the last few days, there was a question here on
inverting a regex without using !~. Your case is even simpler because
it's anchored.

This might work:

        /^(?!8([0876])\1)\d+$/

--
        Bart.



Tue, 27 Jan 2004 20:45:51 GMT  
 Can't Negate Regex /^8([0876])\1\d+$/

Quote:
> I am trying to negate the following regex which I am using
> to filter toll free numbers. The regex seems to work fine as
> is but when I try to negated it won't work.

> $regex =~ /^8([0876])\1\d+$/;

> even though I have tried many different combinations like

> /^[^8([0876])\1]\d+$/
> /^[^8([0876])]\1\d+$/

> I can't get it to work properly. Any help is welcome.

Usually, you would just negate the operation, either by using "!~"
instead of "=~" or by negating the result (eg. use "unless" instead of
"if").

However, this question is often asked in a slightly different context
-- only the regex can change and not the code.  The general answer to
that is to use a negative lookahead.

/^(?!8([0876])\1\d+$)/

Depending on the original regex, it can be more complicated.  In
particular, negating unanchored patterns requires:

/^(?!.*pattern)/;

--
Ren Maddox



Tue, 27 Jan 2004 12:09:07 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. regex negating a search/word

2. negate regex?

3. RegEx and Negated Words

4. Regex question (negating a charater sequence).

5. $query->checkbox_group : Can't modify negate in scalar assignment

6. parsing canned response

7. question about canned perl script

8. Troubles with a Canned script

9. canned reply script

10. canned reply??

11. canned replies

12. OT: Regex - hard or soft 'g'

 

 
Powered by phpBB® Forum Software