首页 > python使用正则表达式去掉字符串中大括号之间的字符

python使用正则表达式去掉字符串中大括号之间的字符

我的代码是这样的:

import re
import sys 

s='{通配符}你好,今天开学了{通配符},你好'
print "s", s
a1 = re.compile('\{.*\}' )
d = a1.sub('',s)
print "d",d

我想把s中的两个{通配符}}给去掉,但是现在的代码执行后d只剩下,你好了,请问应该怎么实现我想要的功能呢,这个正则表达式应该怎么写?


那么如果上面是[],方括号,怎样去除方括号和其中的内容


import re

text = re.sub(r'{[^{}]*}', '', s)  # 去除包含在……}中的内容



用Python 3.4验证过了

pythonimport re
import sys

s='{通配符}你好,今天开学了{通配符},你好'
print("s", s)
a1 = re.compile(r'\{.*?\}' )
d = a1.sub('',s)
print("d",d)
a1 = re.compile(r'\{[^}]*\}' )
d = a1.sub('',s)
print("d",d)

a1 = re.compile('\{.*?\}' )
【热门文章】
【热门文章】