exclude word on a regexp

P

PdG

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?
 
P

PdG

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

(?!my connector is broken)

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

Lew

PdG said:
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\"." );
 
D

Daniele Futtorovic

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\"." );

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

Oliver Wong

Daniele Futtorovic said:
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.

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
 
S

Stefan Ram

PdG said:
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
 
R

Roedy Green

(?!my connector is broken)
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.
that is not a complete regex. You need something in it that DOES
match. Also the () are part of the expression.
 
R

Roedy Green

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

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
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top