Mark J. Reed
11/29/2003 7:41:00 PM
DC = Daniel Carrera
PT = Phil Tomson
DC> Ruby:
DC> str =~ /^(#{char})\s*{\s*(#{char}+),(.*)}/
DC>
DC>
DC> Python:
DC> regex = re.compile(r"^(" + char + "+)\s*{\s*(" + char + "+),(.*)}")
DC> regex.match(str)
DC>
PT> Ouch, that's painful.
Eh, it's typical of languages that don't have a regex literal syntax,
which would be most languages. Although most regex APIs do provide
a one-step version that takes a string, compiles it for you, and
then does the match; and Python is no exception:
re.match("^(" + char + r"+)\s*{\s*(" + char + "+),(.*)}"
Of course, you'd want to use the two-step version if you were going to
use the same regex multiple times, for efficiency's sake.
That leaves the lack of string interpolation as the main annoyance, and many
people, especially those who learned to program with classical languages,
explicit appends much clearer than interpolation, because their eyes sort
of skip right over string literals in "nothing to see here" mode.
(r"..." is Python for '...', btw: strings without special character
sequence processing. The 'r' stands for "raw" strings.)
-Mark