RegExp: how NOT to match string?? 
Author Message
 RegExp: how NOT to match string??

Hi all,

I know how you can match a string with regular expressions, but how
do you match anything EXCEPT some string?

Example: I want to match all occurences of "CD" except where it is
preceded by "AB". This means I want to match "XXCD", "AXCD"
and "XBCD", but not "ABCD".

If I want "CD" except when preceded by the single character "B",
I could use /[^B]CD/g  but for multiple characters /[^A][^B]CD/g
would not work, as this also wouldn't match "AXCD" or "XBCD".

I could try /[^A].CD | .[^B]CD/g  or shorter /([^A]. | .[^B]) CD/g ,
(spaces added for readability)
but for longer preceding strings this would lead to a rather long
specification.

Are there any better methods?

Thanks for any help,

Hans Kesting



Sun, 08 Dec 2002 03:00:00 GMT  
 RegExp: how NOT to match string??

Trying to do exclusions with regular expressions alone can get very
messy, as you have discovered. You're better off using good
old-fashioned string manipulation techniques, as shown below.

var testString = "CDABCDERCDHJLCDPOCDABCDTPCDAKCDE";
var arrX = testString.split("CD");
var arrY = new Array();
var y = 0;
for(var x = 0; x < arrX.length - 1; x++)
    if(!/AB$/.test(arrX[x])) arrY[y++] = arrX[x] + "CD";

alert(arrY.join('\n'));


: Hi all,
:
: I know how you can match a string with regular expressions, but how
: do you match anything EXCEPT some string?
:
: Example: I want to match all occurences of "CD" except where it is
: preceded by "AB". This means I want to match "XXCD", "AXCD"
: and "XBCD", but not "ABCD".
:
: If I want "CD" except when preceded by the single character "B",
: I could use /[^B]CD/g  but for multiple characters /[^A][^B]CD/g
: would not work, as this also wouldn't match "AXCD" or "XBCD".
:
: I could try /[^A].CD | .[^B]CD/g  or shorter /([^A]. | .[^B]) CD/g ,
: (spaces added for readability)
: but for longer preceding strings this would lead to a rather long
: specification.
:
: Are there any better methods?
:
:
: Thanks for any help,
:
: Hans Kesting
:
:
:



Sun, 08 Dec 2002 03:00:00 GMT  
 RegExp: how NOT to match string??

Quote:
> Hi all,

> I know how you can match a string with regular expressions, but how
> do you match anything EXCEPT some string?

> Example: I want to match all occurences of "CD" except where it is
> preceded by "AB". This means I want to match "XXCD", "AXCD"
> and "XBCD", but not "ABCD".

Hi,

In JScript 5.5 (ECMAScript 3) you can use negative assertions:

Match CD except where it is preceded by AB:

var re = /(?!AB)..CD/g;
var s = "XXCD AXCD XBCD ABCD";
var arr = s.match(re);
alert(arr);

Peter

--
Peter J. Torr - Microsoft Windows Script Program Manager

Please do not e-mail me with questions - post them to this
newsgroup instead. Thankyou!



Sat, 14 Dec 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Regexp to match a subset of strings but exclude all of a certain character

2. regexp matches unicode char in string

3. RegExp match of string with square bracket

4. RegExp question: match within another match

5. RegExp Pattern for Matching a URL?

6. RegExp Pattern for Matching a URL?

7. RegExp pattern matching

8. RegExp pattern-matching problem

9. RegExp to match complete words

10. RegExp whitespace matching

11. RegExp - Match ANY repeating pattern

12. RegExp to match complete words

 

 
Powered by phpBB® Forum Software