Re: \l operation in regular expressions
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: \l operation in regular expressions

From: Fredrik Lundh <fredrik@pythonware.com>
Date: Tue May 24 2005 - 16:40:16 CEST

"Kalle Anka" <skromta@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>
Received on Thu Sep 29 16:13:16 2005