\l operation in regular expressions

Fredrik Lundh fredrik at pythonware.com
Tue May 24 10:40:16 EDT 2005


"Kalle Anka" <skromta at gmail.com> wrote:

> I've started to play around with regexps in Python and I tried
> something like this
>
> print re.sub( r'(\bw\w+\b)', r'\u\1', 'will it work')
>
> and get
>
> \uwill it \uwork
>
> when I had expected
>
> Will it Work
>
> I tried to find some documentation about this but I can't find anything
> that says if operations like \l \L \u \U etc are supported or not.

they're not mentioned in the RE syntax chapter, and they don't work, so "not
supported" is probably the right answer.

> It doesn't look like it supported? What is the "Python way" of doing this,
> writing a function and use that as the replacement?

    def fixup(m):
        return m.group().capitalize()
    s = re.sub( r'(\bw\w+\b)', fixup, 'will it work')

or, if you insist on writing one-liners:

    s = re.sub( r'(\bw\w+\b)', lambda m: m.group().capitalize(), 'will it work')

</F> 






More information about the Python-list mailing list