excluding search string in regular expressions

Oliver Fromme olli at haluter.fromme.com
Thu Oct 21 09:28:28 EDT 2004


Mitja <nun at example.com> wrote:
 > Franz Steinhaeusler wrote:
 > > Franz Steinhaeusler wrote:
 > > > [...]
 > > > single characters with [^ab] but I need not(ab)
 > > > 
 > > > not_this_brace_pattern(\*\*/\n).*::
 > > 
 > > Sorry,
 > > is this the solution (simple concatenating
 > > [^*][^*][^/]\n.*:: ?
 > 
 > That should do, though it's admittedly far from elegant; I, too,
 > would like to see a nicer solution.

It won't work correctly.  Franz needs a sub-expression that
matches anything which is not "**/".  However, [^*][^*][^/]
is a character-wise negation, not word-wise.  It doesn't
match "**/", but neither does it match "xx/", nor any other
string which has only one or two of the characters at the
right position.

What you need is a "negative look-behind assertion".  The
following Python-RE will do:  (?<!\*\*/)\n.*::
Remember to use raw string notation, or you need to double
the backslashes:

my_re_str = r"(?<!\*\*/)\n.*::"
my_re_obj = re.compile(my_re_str)

Note that you might want to use \s* instead of \n, so any
amount of whitespace (including newlines) is matched, not
just one single newline.

For more information about regular expressions supported by
Python, refer to the Library Reference manual:

http://docs.python.org/lib/re-syntax.html

Best regards
   Oliver

-- 
Oliver Fromme, Konrad-Celtis-Str. 72, 81369 Munich, Germany

``All that we see or seem is just a dream within a dream.''
(E. A. Poe)



More information about the Python-list mailing list