excluding search string in regular expressions

Franz Steinhaeusler franz.steinhaeusler at utanet.at
Fri Oct 22 10:41:17 CEST 2004


On 21 Oct 2004 13:28:28 GMT, Oliver Fromme <olli at haluter.fromme.com>
wrote:

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

Hello Oliver,

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

yes, you are right, the approach above is false.

>
>What you need is a "negative look-behind assertion".  

??, sounds interesting ;)

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

(?<!...)
    Matches if the current position in the string is not preceded by a
match for..

That is it.

Many thanks for your helpful reply,

-- 
Franz Steinhaeusler



More information about the Python-list mailing list