Two RE proposals

Skip Montanaro skip at pobox.com
Fri Jul 26 17:36:18 EDT 2002


    David> 1. Add a substitution operator - in the example below it's "!<..>"

    David> word = r"\w*"
    David> punct = r"[,.;?]"
    David> wordpunct = re.compile(r"!<word>!<punct>")

How about

    word = r"\w*"
    punct = r"[,.;?]"
    wordpunct = re.compile(r"%(word)s%(punct)s" % locals())

which you can do today?  (I'd also argue that a word would be "\w+".)

    David> 2. Make r"(a|b)*" mean any number of a's or b's. This doesn't
    David>    work, at least in some situations with the current re compiler
    David>    - the "any" op "*" doesn't seem to span over a parened
    David>    group. 

The * doesn't (and shouldn't) operate over grouping parens.  You're asking
it to supply you with a variable number of groups, which it can't do.

Besides, what's wrong with r"([ab]*)"?

-- 
Skip Montanaro
skip at pobox.com
consulting: http://manatee.mojam.com/~skip/resume.html




More information about the Python-list mailing list