regular expression concatination help

Alex Martelli aleax at aleax.it
Tue Sep 18 04:04:57 EDT 2001


"Curtis Jensen" <cjensen at bioeng.ucsd.edu> wrote in message
news:3BA631A4.3DB54406 at bioeng.ucsd.edu...
> I would like to match one regular expression concatenated with another
> regular expression 0 or 1 times. "re0" is the first regular expression,
> "re1" is the second.  re0 includes the empty string; implying that
> "re0+re1?" would include everything that "re1?" includes.  In the
> excerpt below, I would like to match str0+str1, but I can't make it
> happen.
>
> Any help in fixing this would be appreciated.  Thanks.
>
> I have:
> >>> import regex
> >>> str0 = '1,2,31'
> >>> str1= '1.2..2:0.2'
> >>> re0 = '\([0-9],?\)*'

One problem: you are not using raw strings for your re patterns,
so some of the backslashes may be parsed away.  Get into the
habit of always using, e.g., re0 = r'(\d,?)*'.

Each occurrence of [0-9] in these re patterns is better expressed
as \d -- more readable, more concise, maybe a tad faster.

The re0 pattern you give does not match the str0 literal you
give.  Rather, re0 as you give it would need parentheses around
each "digit optionally followed by comma".  I assume you mean
to use ( and ) [parentheses that define re groups] and not the
\( and \) that you actually used [match actual parentheses].

There may be more problems, but when code-inspecting I tend to
stop after locating the first 3 or so potentially-killer issues.
Fix these, and see if things don't become better:-).


Alex






More information about the Python-list mailing list