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 / June 2007

Tip: Looking for answers? Try searching our database.

exclude word on a regexp

Thread view: 
PdG - 20 Jun 2007 09:24 GMT
Sorry, i think it's a faq, but it's driving me crazy.

The probem is very simple: i've a string, and i want to match if there
isn't a substring inside it.

Ie: the string is "my connector is broken", and i want a regexp that
gives true if there isn't the string "connector" inside it, false
otherwise.

the !~/connector/ is the solution, but i've a library where the
matching is done using a  =~ /$regexp/ and i'd like to don't touch the
source.. only write a good $regexp that is identical to  !~/connector/

Can you help me?
Roedy Green - 20 Jun 2007 10:56 GMT
>"my connector is broken",

I have not done it, but the docs suggest this should work

(?!my connector is broken)
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
PdG - 20 Jun 2007 12:27 GMT
> >"my connector is broken",
>
[quoted text clipped - 4 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com

I've tried but it does not work..

i've to exclude the "connector" string, so i need a regexp that gives
true on "my head is broken", and false on "my connector is broken",
using "connector" like key.
Lew - 20 Jun 2007 13:09 GMT
> i've to exclude the "connector" string, so i need a regexp that gives
> true on "my head is broken", and false on "my connector is broken",
> using "connector" like key.

String s = "my connector is broken";
Pattern p = Pattern.compile( ".*connector.*" );
Matcher m = p.matcher( s );
boolean lacksConnector = ! m.matches();
System.out.println( s +(lacksConnector? " does " : " does not ")
                   +"contain \"connector\"." );

s = "my head is broken";
m = p.matcher( s );
lacksConnector = ! m.matches();
System.out.println( s +(lacksConnector? " does " : " does not ")
                   +"contain \"connector\"." );

Signature

Lew

Daniele Futtorovic - 20 Jun 2007 13:36 GMT
>> i've to exclude the "connector" string, so i need a regexp that gives
>> true on "my head is broken", and false on "my connector is broken",
[quoted text clipped - 12 lines]
> System.out.println( s +(lacksConnector? " does " : " does not ")
>                    +"contain \"connector\"." );

Check out what the OP wrote earlier... He's got preexiting logic which
uses a regex, wants to change but the regex and leave the rest intact.

I don't think what the OP intends is feasible. That's not quite what
regexes are for: matching the absence of something. Indeed, why should
they, when it is so easy to match the presence of something and then
negate the result. But he'll have to change the logic for that, and I'm
afarid the OP has no other option.
Oliver Wong - 20 Jun 2007 23:14 GMT
>>> i've to exclude the "connector" string, so i need a regexp that gives
>>> true on "my head is broken", and false on "my connector is broken",
[quoted text clipped - 21 lines]
> negate the result. But he'll have to change the logic for that, and I'm
> afarid the OP has no other option.

   It's certainly possible, but painful.

   The proof that it's possible is that it's fairly simple to draw a
finite state machine (FSM) which accepts all strings which do not contain
"connector", and reject all strings that do, and it's always possible to
convert a FSM into a regular expression. But it's painful to write. It'd
look something like:

(anything except 'c')
OR "c" followed by (anything except 'o')
OR "co" followed by (anything except 'n')

and so on.

   - Oliver
Roedy Green - 22 Jun 2007 11:31 GMT
> (?!my connector is broken)
>> --
[quoted text clipped - 6 lines]
>true on "my head is broken", and false on "my connector is broken",
>using "connector" like key.
that is not a complete regex.  You need something in it that DOES
match.  Also the () are part of the expression.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Stefan Ram - 21 Jun 2007 00:51 GMT
>Ie: the string is "my connector is broken", and i want a regexp
>that gives true if there isn't the string "connector" inside
>it, false otherwise.

 I do not read Google postings on a regular base, so my answer
 is a little bit late, but it took me just a minute to type it
 in - so it is not that difficult as someone wrote.

public class Main
{ public static void test( final java.lang.String text )
 { java.lang.System.out.println
   ( java.util.regex.Pattern.matches
     ( "(?!my connector)my [a-z]* is broken", text )); }

 public static void main( final java.lang.String[] args )
 { test( "my connector is broken" );
   test( "my engine is broken" );
   test( "my engine is working" ); }}

false
true
false
Roedy Green - 22 Jun 2007 11:59 GMT
>  I do not read Google postings on a regular base, so my answer
>  is a little bit late, but it took me just a minute to type it
[quoted text clipped - 14 lines]
>true
>false

thanks for his example.  I have modified it a bit and added it to the
Java glossary at http://mindprod.com/jgloss/regex.html#NEGATIVE
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


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.