Python正则表达式无法删除[%~abcd~%]之间的内容

时间:2017-12-01 11:19:55

标签: python regex

我有原始的HTML,我试图从输出字符串中删除这个整个块[%〜as..abcd~%]。使用py库的重新编译

SQL Server Package

代码有什么问题?

2 个答案:

答案 0 :(得分:1)

你的模式应该是

cleanM = re.compile(r'\[\%\~ .*? \~\%\]',re.S)

.匹配除新换行符之外的任何字符,S允许匹配换行符

答案 1 :(得分:0)

你需要使用[\S\s]*代替.*,你可以省略编译:

import re
teststring = '''Check the direction . [%~ MACRO wdwDate(date) BLOCK;
                 SET tmpdate = date.clone();
                 END ~%] Determine if both directions.'''
scleantext = re.sub('(\[%~ [\S\s]* ~%\])', '', teststring)

print(scleantext)