I'm a bit stuck on how to not match something in a reg ex,
Ie, if I wanted to test that a string did not begin with ABC
Do I have to use a character class and do something like this
[^(ABC)]
???
hiwa - 09 Jul 2006 10:33 GMT
sks :
> I'm a bit stuck on how to not match something in a reg ex,
>
[quoted text clipped - 4 lines]
>
> ???
Basically and generally, regular expression can't do it.
But your Java program that utilizes regex could do.
Jeffrey Schwab - 09 Jul 2006 16:41 GMT
> I'm a bit stuck on how to not match something in a reg ex,
>
> Ie, if I wanted to test that a string did not begin with ABC
>
> Do I have to use a character class and do something like this
> [^(ABC)]
No. The character-class will match one character that is none of A, B,
C, or either parenthesis. Try this:
Pattern.compile("^(?!ABC)");
Oliver Wong - 10 Jul 2006 15:48 GMT
> I'm a bit stuck on how to not match something in a reg ex,
>
[quoted text clipped - 4 lines]
>
> ???
Your regular expression would basically say this (in plain English):
( String does NOT start with A. )
OR
( String starts with A, but does NOT have a B following it)
OR
( String starts with AB, but does not have a C following it)
I'm not familiar with Java's specific syntax for regular expressions,
but it'd probably look something like:
([^A])|(A[^B])|(AB[^C])
possibly with the addition of "string start" and "string end" markers.
- Oliver