Reg ex and not match

S

sks

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)]

???
 
H

hiwa

sks ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
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)]

???
Basically and generally, regular expression can't do it.
But your Java program that utilizes regex could do.
 
J

Jeffrey Schwab

sks said:
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)");
 
O

Oliver Wong

sks said:
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)]

???

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top