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
...
> 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

Signature
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Raymond DeCampo - 03 Aug 2005 01:49 GMT
> ...
>
[quoted text clipped - 35 lines]
>
> /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

Signature
XML is the programmer's duct tape.
kevinm3574 - 03 Aug 2005 17:04 GMT
HEY HEY!!! This worked great..thanks for the reply Lasse.
Kevin
> 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
[quoted text clipped - 7 lines]
>
> .*(?!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

Signature
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/