简体   繁体   中英

Python RegEx matches multi-line substring, but will not replace it

This seems like an easy problem, but obviously there is something I am missing.

I have a Python function designed to replace chunks of code that occur between custom tags with HTML formatted code:

def subCode(text):
    tags = re.findall('<<<mytag>>>.+?<<</mytag>>>', text, re.S)
    for tag in tags:
        match = re.search('>>>(.+?)<<<', tag, re.S)
        replaced_code = replaceCode(match.group(1))
        text = re.sub(tag, replaced_code, text, re.S|re.M)
    return text

This will match the code that falls in between the tags, like here:

this is some 
random text
<<<mytag>>>now this
   is some
   random code<<</mytag>>>
and this is text again

But it is not replacing the code with the formatted replacement, and the returned string is identical to the input. What am I missing?

I think you want to use the variant of re.sub() that takes a function as the second argument, it's much simpler:

def subCode(text):
    return re.sub('<<<mytag>>>(.+?)<<</mytag>>>', replaceFunc, text, flags=re.S)

def replaceFunc(match):
    return replaceCode(match.group(1))

If the second argument to re.sub() is a function, it takes a match object as input and is expected to return the replacement string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM