python/regex question... hope someone can help

John Machin sjmachin at lexicon.net
Sun Dec 9 05:13:35 EST 2007


On Dec 9, 6:13 pm, charonzen <your.mas... at gmail.com> wrote:

The following *may* come close to doing what your revised spec
requires:

import re
def ch_replace2(alist, text):
    for bigram in alist:
        pattern = r'\b' + bigram.replace('_', ' ') + r'\b'
        text = re.sub(pattern, bigram, text)
    return text

Cheers,
John



More information about the Python-list mailing list