Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2005

Tip: Looking for answers? Try searching our database.

java regex equiv to perl !~

Thread view: 
kevinm3574 - 02 Aug 2005 17:00 GMT
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
jan V - 02 Aug 2005 17:29 GMT
> in perl I can say:
>
[quoted text clipped - 7 lines]
> 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... )
kevinm3574 - 02 Aug 2005 19:33 GMT
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
Lasse Reichstein Nielsen - 02 Aug 2005 22:33 GMT
...
> 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
Thomas Weidenfeller - 03 Aug 2005 09:04 GMT
> 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/

Alan Moore - 03 Aug 2005 04:38 GMT
>in perl I can say:
>
[quoted text clipped - 11 lines]
>
>Kevin

The Java equivalent would be:

 String text = "abc def geh";
 Pattern p = Pattern.compile("def");
 Matcher m = p.matcher(text);
 if ( !m.find() ) {
     ...
 }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.