Java Regex: How to "NOT" a group?

D

danelaverty

I've looked at several Java regex tutorials, but haven't found a way to
do:

!regexString.equals(inputString);

using only regular expressions. I see that [^X] will return true for
any character that is not X, but what if I want the string that is not
"XYZ"? Entering [^XYZ] or [^(XYZ)] return true for any character that
is not X, Y, OR Z rather than any string that is not XYZ together. I
hope I've explained this clearly. Can anyone tell me how to use Java
regex for !String rather than !character?

Thanks
 
H

hiwa

(e-mail address removed) ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
I've looked at several Java regex tutorials, but haven't found a way to
do:

!regexString.equals(inputString);

using only regular expressions. I see that [^X] will return true for
any character that is not X, but what if I want the string that is not
"XYZ"? Entering [^XYZ] or [^(XYZ)] return true for any character that
is not X, Y, OR Z rather than any string that is not XYZ together. I
hope I've explained this clearly. Can anyone tell me how to use Java
regex for !String rather than !character?

Thanks
Matching to the negative !X, if it was supported, couldn't have an
explicit and definitive boundary so regex engine could go wild.
Therefore, it is not supported from the beginning.
 
J

Jeffrey Schwab

I've looked at several Java regex tutorials, but haven't found a way to
do:

!regexString.equals(inputString);

using only regular expressions. I see that [^X] will return true for
any character that is not X, but what if I want the string that is not
"XYZ"? Entering [^XYZ] or [^(XYZ)] return true for any character that
is not X, Y, OR Z rather than any string that is not XYZ together. I
hope I've explained this clearly. Can anyone tell me how to use Java
regex for !String rather than !character?

You can check that you're not looking at a match to a given pattern via
(?!). This won't actually consume any characters, since the characters
didn't match.

(?!XYZ)

Does this cover it? If not, what specifically are you doing?
 

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,266
Messages
2,571,078
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top