splitting perl-style find/replace regexp using python

Peter Otten __peter__ at web.de
Thu Mar 1 11:12:24 CET 2007

John Pye wrote:

> I have a file with a bunch of perl regular expressions like so:
> /(^|[\s\(])\*([^ ].*?[^ ])\*([\s\)\.\,\:\;\!\?]|$)/$1'''$2'''$3/ #
> bold
> /(^|[\s\(])\_\_([^ ].*?[^ ])\_\_([\s\)\.\,\:\;\!\?]|$)/$1''<b>$2<\/
> b>''$3/ # italic bold
> /(^|[\s\(])\_([^ ].*?[^ ])\_([\s\)\.\,\:\;\!\?]|$)/$1''$2''$3/ #
> italic
> These are all find/replace expressions delimited as '/search/replace/
> # comment' where 'search' is the regular expression we're searching
> for and 'replace' is the replacement expression.
> Is there an easy and general way that I can split these perl-style
> find-and-replace expressions into something I can use with Python, eg
> re.sub('search','replace',str) ?
> I though generally it would be good enough to split on '/' but as you
> see the <\/b> messes that up. I really don't want to learn perl
> here :-)

How about matching all escaped chars and '/', and then throwing away the

def split(s):
    breaks = re.compile(r"(\\.)|(/)").finditer(s)
    left, mid, right = [b.start() for b in breaks if b.group(2)]
    return s[left+1:mid], s[mid+1:right]


More information about the Python-list mailing list