Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inverting regular expression

 
manuel aldana
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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...
 
manuel aldana
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
autsch,

stupid mistake from my side.
thank you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic