negating words with Java regular expressions

R

Raj Ashar

All, I would like to know if Java's implementation of regular
expressions allows for the negation of a word. I know that it is
possible to negate members of a character class with "[^...]" to avoid
matching against those characters, and that it is possible to match
for a string by specifying the members of the string in a regular
expression (eg "my first regex"), but I'm not sure if there's a way to
match selectively (ie match all instances of ";" except where ".;.").

Thank you.

Raj
 
C

Chris Smith

Raj said:
but I'm not sure if there's a way to
match selectively (ie match all instances of ";" except where ".;.").

Pattern.compile("[^\\.]\\;[^\\.]");

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Carl Howells

Chris said:
Raj said:
but I'm not sure if there's a way to
match selectively (ie match all instances of ";" except where ".;.").


Pattern.compile("[^\\.]\\;[^\\.]");

Nope. That won't do. You're matching all instances of
"<something>;<something>", except when both <something>s are ".". But
that isn't what was requested. That won't match either semicolon in the
string "; hello! ;", for instance. It also grabs the characters on
either side of the semicolon, which is not what was requested, either.

Try:

Pattern.compile("(?<!\\.);(?!\\.)");

I haven't tested it, but it should work.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top