我有字符串:
...<w:t> Name</w:t></w:r><w:r><w:rPr><w:rFonts w:ascii="Cambria" w:hAnsi="Cambria"/><w:b/><w:sz w:val="28"/><w:szCs w:val="28"/></w:rPr><w:t>:</w:t></w:r><w:r><w:rPr></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:r><w:rPr><w:b/><w:bCs/></w:rPr><w:t>{{</w:t></w:r><w:r><w:rPr></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:r><w:rPr><w:i/><w:iCs/></w:rPr><w:t>test</w:t></w:r><w:r><w:rPr><w:i/><w:iCs/></w:rPr><w:t>.name</w:t></w:r><w:r><w:rPr></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:r><w:rPr><w:b/><w:bCs/></w:rPr><w:t>}} <w:t>....
我需要一个脚本来删除 {{ 和 }} 之间的所有标签(<...>) 但不要删除字符对之间的内容,例如:
The result of:
{{ <wr> test.name1 <wr> }} <wr><wr> {{ <wr> test.name2 <wr> }}
will be:
{{ test.name1 }} <wr><wr> {{ test.name2 }}
not:
{{ test.name1 }} {{ test.name2 }}
提前谢谢您!
最佳答案
如果您不需要单个正则表达式,您可以组合替换:
import re
s='{{ <wr> test.name1 <wr> }} <wr><wr> {{ <wr> test.name2 <wr> }}'
re.sub(r'({{[^{}]+}})', lambda x: re.sub(r'<[a-zA-Z0-9:-]+>', '', x.group(0)), s)
'{{ test.name1 }} <wr><wr> {{ test.name2 }}'
关于python - 使用python和regexp删除字符串中字符串之间的所有xml标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35914878/