rawstrings are optimized for use as RE literals (was Re: Is this a bug?)

Alex Martelli aleaxit at yahoo.com
Tue May 15 03:09:05 EDT 2001


"Bengt Richter" <bokr at accessone.com> wrote in message
news:3b0090df.123507724 at wa.news.verio.net...
    ...
> >[Alex Martelli, in reply to Costas Menico]
    ...
> >> Rawstrings are optimized for use as regular-expression literals, where
    ...
> Actually, what is the purpose of recognizing the backslash as
> an escape in raw strings, other than compatibility with C-style
> strings that happen to include the delimiter character? Other

"Once again, with feeling":

    rawstrings are optimized for use as regular-expression literals.

Hey, maybe if I keep writing that often enough, and quoting and
re-quoting it each and every time, somebody will notice it...?-)

Naah, better make it the Subject as well.  As the Library Ref
says in 4.2.1, Regular Expression Syntax, "it's highly recommended
that you use raw strings for all but the simplest expressions".

Say I want to RE-match strings like:
    Some "Joe's Diner" kinda place
with any word (as a group) in lieu of 'Joe'.  With rawstrings:
    r'Some "(\w+)\'s Diner" kinda place'
is as reasonably clear and compact as RE's ever get.  Without
rawstrings, or if a rawstring were unable to escape its own
delimiter, it WOULD be messier.

You might want to think of the 'r' as standing for 'regular
expression' rather than 'raw', if it helps your memory!-)


Alex






More information about the Python-list mailing list