Regular Expressions: Negating bracketed strings 
Author Message
 Regular Expressions: Negating bracketed strings

I've had a good look round the FAQs, but I can't work this one out.

I want to find every occurrence of ABC _not_ followed by XX\d+ (e.g.
ABCXX123 is fine, ABCXY123 is not).  I tried using this:

/ABC[^(XX\d+)]/

which doesn't work - I guess everything within [] is just being treated
as a character class, so the parentheses and + lose their meta-ness.

I can see how to solve this programmaticlly (i.e. search for ABC, then
check XX\d+ for each one found), but this would mean I'd be looping in
Perl for every _valid_ use of ABC, whereas I only want to process
"errors".

Does anyone know how to do it in a single expression?

Thanks,
John Edwards




Sat, 11 Mar 2000 03:00:00 GMT  
 Regular Expressions: Negating bracketed strings

On Tue, 23 Sep 1997 11:58:54 +0100,

Quote:

> I've had a good look round the FAQs, but I can't work this one out.

> I want to find every occurrence of ABC _not_ followed by XX\d+ (e.g.
> ABCXX123 is fine, ABCXY123 is not).  I tried using this:

Look for "lookahead" in the perlre (regular expressions) manpage

--



Sat, 11 Mar 2000 03:00:00 GMT  
 Regular Expressions: Negating bracketed strings

Quote:

> I want to find every occurrence of ABC _not_ followed by XX\d+

    /ABC(?!XX\d+)/

Is that what you want?

Quote:
> (e.g.  ABCXX123 is fine, ABCXY123 is not).

That sounds backwards from what I just did...  Hope this helps!

--
Tom Phoenix           http://www.teleport.com/~rootbeer/

Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/
              Ask me about Perl trainings!



Sun, 12 Mar 2000 03:00:00 GMT  
 Regular Expressions: Negating bracketed strings

Quote:
>I want to find every occurrence of ABC _not_ followed by XX\d+ (e.g.
>ABCXX123 is fine, ABCXY123 is not).  I tried using this:

>/ABC[^(XX\d+)]/

>which doesn't work - I guess everything within [] is just being treated
>as a character class, so the parentheses and + lose their meta-ness.

um... ABCXX123 is FINE?  but by your regex, it looks like you DON'T want
XX\d+

well... this should work:
/ABC(?!XX\d+)/

The (?!...) is the negative look-ahead operator... it means: when whatever
is NOT followed by whatever is here in parens ().

The opposite is (?=...).  it means: when whatever IS followed by whatever
is here in parens ()...

check out "man perlre" for more on this.

----------------
| "I don't contemplate it, I just sit and think about it."
|       - Sonia Balsky
----------------



** I can be found on #perl on irc.ais.net as jpinyan **

- geek code -
GCS/IT d- s>+: a--- C+>++ UAIS+>$ P+++$>++++ L E--->---- W++$
N++ !o K--? w>+ !O M>- V-- PS PE+ !Y !PGP t+ !5 X+ R tv+ b>+
DI+++ D+>++ G>++ e- h- r y?



Sun, 12 Mar 2000 03:00:00 GMT  
 Regular Expressions: Negating bracketed strings

   I've had a good look round the FAQs, but I can't work this one out.

   I want to find every occurrence of ABC _not_ followed by XX\d+ (e.g.
   ABCXX123 is fine, ABCXY123 is not).  I tried using this:

[snip]

   Does anyone know how to do it in a single expression?

(?! ) specifies a zero-width negative lookahead, e.g.

/ABC(?!XX\d+)/

-Jon

------------------------------------
Jon Orwant            http://tpj.com
Editor & Publisher, The Perl Journal



Sat, 18 Mar 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Negating (and retaining match) of a regular expression

2. Regular expression, negating sequence

3. Negating regular expression challenge

4. Use of brackets () in regular expressions

5. Bracketed regular expressions are'nt working in the perl debugger

6. regular expression watching bracket hierarchy?

7. regular expression to extract a string between 2 other strings

8. regular expression - cutting leading zeros from a string

9. regular expression replace string to pad leading zeros

10. regular expression-search odd number or even number of characters in a string

11. question on string manipulation/regular expressions

12. regular expression function to find multiple strings

 

 
Powered by phpBB® Forum Software