negating words with Java regular expressions

Discussion in 'Java' started by Raj Ashar, Jul 13, 2004.

  1. Raj Ashar

    Raj Ashar Guest

    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
    Raj Ashar, Jul 13, 2004
    #1
    1. Advertising

  2. Raj Ashar

    Chris Smith Guest

    Raj Ashar wrote:
    > 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
    Chris Smith, Jul 13, 2004
    #2
    1. Advertising

  3. Raj Ashar

    Carl Howells Guest

    Chris Smith wrote:
    > Raj Ashar wrote:
    >
    >>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.
    Carl Howells, Jul 13, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eduardo Bezerra

    Negating a List Of Numbers

    Eduardo Bezerra, Apr 13, 2005, in forum: C++
    Replies:
    19
    Views:
    523
    Jean-Sebastien Samson
    Apr 20, 2005
  2. Alex Fraser
    Replies:
    8
    Views:
    402
    Joe Wright
    Mar 8, 2006
  3. kwest

    Negating a regular expression

    kwest, Nov 17, 2005, in forum: Perl Misc
    Replies:
    12
    Views:
    188
    Brian McCauley
    Nov 18, 2005
  4. Luke Matuszewski
    Replies:
    8
    Views:
    127
    Luke Matuszewski
    Apr 22, 2006
  5. Noman Shapiro
    Replies:
    0
    Views:
    232
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page