Regular expression help

Alex Martelli aleaxit at yahoo.com
Thu May 3 11:53:11 EDT 2001


[posted AND mailed because of urgency]

"Daniel Klein" <danielk at aracnet.com> wrote in message
news:rcl2ft0flqof5nhc2kbck2q4sqj648qlls at 4ax.com...
> Help! I've _never_ done a regular expression and I've got an emergency
> situation where I need to find all occurrences of
>
> string1 + '(' + string2 + '<' + string3 + '>)'
>
> and there could be spaces betwixt any of the components.
>
> For example,
>
> abc123(def456<ghi789>)
> or
> X   (   Y   <  Z   >   )
>
> Any immediate help appreciated. In the mean time, I'm going to try to work
it
> out for meself.

Assuming you mean for string1, string2, and string3 to be various
possible constant strings:

def matcherOf(string1, string2, string3)
    return re.compile( re.escape(string1) + r'\s*\(\s*' +
        re.escape(string2) + r'\s*\<\s*' + re.escape(string3) +
        r'\s*\>')

Assuming you mean something else, you just need to substitute
the appropriate pieces for these calls to re.escape.  For
example, if you mean that each 'stringX' is an arbitrary
non-empty sequence of word-characters (letters, digits,
underscores) and you want each to be within an unnamed
match-group, the literal string:

    r'(\w+)\s*\(\s*(\w+)\s*\<\s*(\w+)\s*\>'

should be re.compile()able to yield what you desire.


Alex






More information about the Python-list mailing list