wood burning stoves 2.0*
The moose likes Other Languages and the fly likes inverting regular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Languages » Other Languages
Bookmark "inverting regular expression " Watch "inverting regular expression " New topic
Author

inverting regular expression

manuel aldana
Ranch Hand

Joined: Dec 29, 2005
Posts: 308
hi,

i am using following regex to "invert" a pattern (in python):

pattern=[^b][^a][^r]

this gets a bit difficult to read if "inverted" words are getting longer. is there a meta symbol so it possible to write:

~(bar) which matches all pattern bits which do not contain bar.

thanks.
[ April 17, 2006: Message edited by: manuel aldana ]

aldana software engineering blog & .more
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[manuel]: this gets a bit difficult to read

Also, it doesn't work. For example, the pattern [^b][^a][^r] match "xbar" (1st char is not b, 2nd char is not a, 3rd char is not r), and it won't match "boo" or "cat". (Because of the b in "boo" and the a in "cat", respectively.)

I believe you want something using negative lookahead, like

pattern=(?!.*bar)

This matches only if it is not (!) possible to find "bar" after skipping an unknown number of characters (.*).

Hope that helps...


"I'm not back." - Bill Harding, Twister
manuel aldana
Ranch Hand

Joined: Dec 29, 2005
Posts: 308
autsch,

stupid mistake from my side.
thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inverting regular expression