我正在解析一个文件的行,我想删除" {%"和"%}",因为它们代表评论。
更具体地说,是一个字符串,如
bla{% comment %} bli {% useless %}blu
应该返回
bla bli blu
我尝试使用正则表达式,删除了{% .* %}
匹配的所有内容:
import re
s = 'bla{% comment %} bli {% useless %}blu'
regexp = '{% .* %}'
comments = re.findall(regexp, s)
for comment in comments:
s = s.replace(comment, '')
print s
这会blablu
并删除bli
。虽然我理解为什么它会像那样,但我不知道如何获得blabliblu
。
答案 0 :(得分:6)
您需要.*?
。你的点是greedy。
regexp = '{% .*? %}'
当运营商贪婪时,尽可能多地使用""仍然会产生匹配,这意味着它从第一个{%
到最后一个%}
bla{% comment %} bli {% useless %}blu
^ here ... ^ to here
当操作员懒惰时,尽可能少""并且仍然会产生匹配,这意味着它会从{%
转到下一个 %}
。
最好不要显式添加空格,因为模式不匹配没有空格的注释:
regexp = '{%.*?%}'
答案 1 :(得分:2)
您应该使用re.sub()
并使正则表达式非贪婪添加?
。
import re
s = 'bla{% comment %} bli {% useless %}blu'
regexp = '{% .*? %}'
s = re.sub(regexp, "", s)
print(s) # bla bli blu
答案 2 :(得分:0)
这只是解释,因为长度是答案!
懒惰替代(不使用点。)
{% [^\W]+ %}
{% [^\W]* %}
{% [^\W]+? %}
{% [^\W]*? %}
{% [\w]+ %}
懒惰变化(不使用星号)
{% .+? %}