Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java regex equiv to perl !~

Reply
Thread Tools

java regex equiv to perl !~

 
 
kevinm3574
Guest
Posts: n/a
 
      08-02-2005
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

 
Reply With Quote
 
 
 
 
jan V
Guest
Posts: n/a
 
      08-02-2005

"kevinm3574" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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... )


 
Reply With Quote
 
 
 
 
kevinm3574
Guest
Posts: n/a
 
      08-02-2005
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

 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      08-02-2005
"kevinm3574" <(E-Mail Removed)> 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 - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Raymond DeCampo
Guest
Posts: n/a
 
      08-03-2005
Lasse Reichstein Nielsen wrote:
> "kevinm3574" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Alan Moore
Guest
Posts: n/a
 
      08-03-2005
On 2 Aug 2005 09:00:04 -0700, "kevinm3574" <(E-Mail Removed)>
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() ) {
...
}

 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      08-03-2005
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/...g/java/gui/faq
http://www.uni-giessen.de/faq/archiv....java.gui.faq/
 
Reply With Quote
 
kevinm3574
Guest
Posts: n/a
 
      08-03-2005
HEY HEY!!! This worked great..thanks for the reply Lasse.

Kevin

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
C regex equiv to Python implementation? techiepundit@futurepundit.com Python 3 01-08-2006 12:59 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
Ruby equiv of Perl's HTML::Template module? ChrisO Ruby 8 09-15-2004 10:28 PM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments