java regex equiv to perl !~

K

kevinm3574

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
 
J

jan V

kevinm3574 said:
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... )
 
K

kevinm3574

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
 
L

Lasse Reichstein Nielsen

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

Raymond DeCampo

Lasse said:
...
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
 
A

Alan Moore

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() ) {
...
}
 
T

Thomas Weidenfeller

kevinm3574 said:
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
 

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
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top