java regex equiv to perl !~

Discussion in 'Java' started by kevinm3574, Aug 2, 2005.

  1. kevinm3574

    kevinm3574 Guest

    in perl I can say:

    $text = "abc def geh";
    if ($text !~ m/def/) {
    ....
    }

    Obviously, I would fill $text "dynamically" from reading some kind of
    input and then test it for not having the string "def" and then do
    something but I'm having problems figuring out how to do this in Java
    (I hope I'm not just being dumb here). Any help greatly appreciated.

    Thanks.

    Kevin
    kevinm3574, Aug 2, 2005
    #1
    1. Advertising

  2. kevinm3574

    jan V Guest

    "kevinm3574" <> wrote in message
    news:...
    > in perl I can say:
    >
    > $text = "abc def geh";
    > if ($text !~ m/def/) {
    > ....
    > }
    >
    > Obviously, I would fill $text "dynamically" from reading some kind of
    > input and then test it for not having the string "def" and then do
    > something but I'm having problems figuring out how to do this in Java
    > (I hope I'm not just being dumb here). Any help greatly appreciated.


    String's indexOf() returns -1 if a substring is not present. Would that be
    what you're after? (that's not a regex solution though... )
    jan V, Aug 2, 2005
    #2
    1. Advertising

  3. kevinm3574

    kevinm3574 Guest

    Well, no, that's not going to work for what I'm doing.

    I've got an application that takes data that comes in and displays it
    to the end user and a form that allows the user to "filter" what he
    sees (basically does a Pattern.compile of a jtextfield to verify a
    valid regex pattern is input and then that pattern is applied against
    the incoming data). This works great where I'm doing inclusive
    filtering (you know, like, display data that includes the word "goat")
    but I can't seem to get the syntax of the regex correct such that
    exclusive filtering works (such as display all data except that with
    the word "lion"). I've just tried the following:

    ..*(?!lion).*

    thinking that it would filter out data coming in with "anything" and
    the word lion embedded but that didn't work either.

    Thanks.

    Kevin
    kevinm3574, Aug 2, 2005
    #3
  4. "kevinm3574" <> writes:

    ....
    > that pattern is applied against the incoming data). This works
    > great where I'm doing inclusive filtering (you know, like, display
    > data that includes the word "goat") but I can't seem to get the
    > syntax of the regex correct such that exclusive filtering works
    > (such as display all data except that with the word "lion").


    It's generally hard to make a regular expression for not matching a
    word. Regular expressions try their hardest to match a string, and
    succeedes if there is just one way to do it. It's an existential
    proposition (there exists a way to match). You want a regular
    expression to make sure that there is *no* match. That's a universal
    propsition (for all ways to match, it doesn't work). So from a logic
    point of view, it's not the best tool :)

    > I've just tried the following:
    >
    > .*(?!lion).*


    A single Negative lookahead is not the way to do it. It matches if
    there is just *one* match where the lookahed isn't "lion". Like, in
    the string "lion", just after the "l", where "ion" isn't "lion".

    You need to make negative lookehead at each and every position.

    That can be done a little simple by doing negative lookahead
    after each "l":

    ^([^l]*l(?!ion))*[^l]*$

    This requires that all "l"'s occuring must not be followed by "ion".

    I'm not sure how to write a regular expression without lookahead
    that also only matches lines without "lion". I'm sure it's possible,
    and I could probably calculate one from a finite state machine,
    but it's likely to be *quite* big and completely unreadable.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Aug 2, 2005
    #4
  5. Lasse Reichstein Nielsen wrote:
    > "kevinm3574" <> writes:
    >
    > ...
    >
    >>that pattern is applied against the incoming data). This works
    >>great where I'm doing inclusive filtering (you know, like, display
    >>data that includes the word "goat") but I can't seem to get the
    >>syntax of the regex correct such that exclusive filtering works
    >>(such as display all data except that with the word "lion").

    >
    >
    > It's generally hard to make a regular expression for not matching a
    > word. Regular expressions try their hardest to match a string, and
    > succeedes if there is just one way to do it. It's an existential
    > proposition (there exists a way to match). You want a regular
    > expression to make sure that there is *no* match. That's a universal
    > propsition (for all ways to match, it doesn't work). So from a logic
    > point of view, it's not the best tool :)
    >
    >
    >>I've just tried the following:
    >>
    >>.*(?!lion).*

    >
    >
    > A single Negative lookahead is not the way to do it. It matches if
    > there is just *one* match where the lookahed isn't "lion". Like, in
    > the string "lion", just after the "l", where "ion" isn't "lion".
    >
    > You need to make negative lookehead at each and every position.
    >
    > That can be done a little simple by doing negative lookahead
    > after each "l":
    >
    > ^([^l]*l(?!ion))*[^l]*$
    >
    > This requires that all "l"'s occuring must not be followed by "ion".
    >
    > I'm not sure how to write a regular expression without lookahead
    > that also only matches lines without "lion". I'm sure it's possible,
    > and I could probably calculate one from a finite state machine,
    > but it's likely to be *quite* big and completely unreadable.
    >
    > /L


    Maybe I'm being simplistic, but is there a reason you can't simply write
    a regular expression to match lion and then accept the lines that do not
    match and reject the lines that do?

    Ray

    --
    XML is the programmer's duct tape.
    Raymond DeCampo, Aug 3, 2005
    #5
  6. kevinm3574

    Alan Moore Guest

    On 2 Aug 2005 09:00:04 -0700, "kevinm3574" <>
    wrote:

    >in perl I can say:
    >
    >$text = "abc def geh";
    >if ($text !~ m/def/) {
    > ....
    >}
    >
    >Obviously, I would fill $text "dynamically" from reading some kind of
    >input and then test it for not having the string "def" and then do
    >something but I'm having problems figuring out how to do this in Java
    >(I hope I'm not just being dumb here). Any help greatly appreciated.
    >
    >Thanks.
    >
    >Kevin


    The Java equivalent would be:

    String text = "abc def geh";
    Pattern p = Pattern.compile("def");
    Matcher m = p.matcher(text);
    if ( !m.find() ) {
    ...
    }
    Alan Moore, Aug 3, 2005
    #6
  7. kevinm3574 wrote:
    > I've got an application that takes data that comes in and displays it
    > to the end user and a form that allows the user to "filter" what he
    > sees (basically does a Pattern.compile of a jtextfield to verify a
    > valid regex pattern is input and then that pattern is applied against
    > the incoming data). This works great where I'm doing inclusive
    > filtering (you know, like, display data that includes the word "goat")
    > but I can't seem to get the syntax of the regex correct such that
    > exclusive filtering works (such as display all data except that with
    > the word "lion"). I've just tried the following:
    >
    > .*(?!lion).*


    if(!Pattern.matches("lion", "your input") {
    //
    // There is no lion in the input
    //
    }

    Note the '!' in the condition. It's not much different than using !~
    instead of =~ in Perl.

    For more advanced checks you might want to use an explicit Matcher and
    fiddle with the matching region.

    /Thomas

    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
    Thomas Weidenfeller, Aug 3, 2005
    #7
  8. kevinm3574

    kevinm3574 Guest

    HEY HEY!!! This worked great..thanks for the reply Lasse.

    Kevin
    kevinm3574, Aug 3, 2005
    #8
    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. Replies:
    3
    Views:
    272
    Jorgen Grahn
    Jan 8, 2006
  2. ChrisO
    Replies:
    8
    Views:
    160
    ChrisO
    Sep 15, 2004
  3. Tom Allison

    perl equiv.

    Tom Allison, Feb 20, 2006, in forum: Ruby
    Replies:
    3
    Views:
    103
    Austin Ziegler
    Feb 26, 2006
  4. snacktime

    ruby equiv of perl pos

    snacktime, Aug 12, 2006, in forum: Ruby
    Replies:
    14
    Views:
    198
    Thomas, Mark - BLS CTR
    Aug 17, 2006
  5. Amittai Aviram

    Perl equiv to PHP file() ?

    Amittai Aviram, Aug 18, 2003, in forum: Perl Misc
    Replies:
    4
    Views:
    107
    Gunnar Hjalmarsson
    Aug 18, 2003
Loading...

Share This Page