This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
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 Head First Android this week in the Android forum!
JavaRanch » Java Forums » Languages » Other Languages
Bookmark "inverting regular expression " Watch "inverting regular expression " New topic

inverting regular expression

manuel aldana
Ranch Hand

Joined: Dec 29, 2005
Posts: 308

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


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.

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

aldana software engineering blog & .more
Jim Yingst

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


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

stupid mistake from my side.
thank you.
I agree. Here's the link:
subject: inverting regular expression
It's not a secret anymore!