python去除特定字符串之间的内容
import re
def remove_identifiers_and_content(original_string, identifier_A, identifier_B):
# 将标识符A和B转换为正则表达式,以匹配它们及其之间的任何内容
pattern = f"{identifier_A}.*?{identifier_B}"
# 使用re.sub()函数替换掉匹配的模式
modified_string = re.sub(pattern, "", original_string, flags=re.DOTALL)
return modified_string
if __name__ == '__main__':
content = """
```searchSource
[{"index":1,"source":"ht。"}]
```
具体介绍:
"""
# sub = re.sub('```searchSource.*?```', '', content)
sub = remove_identifiers_and_content(content, '```searchSource', '```', )
print(sub)
python使用正则表达式去掉字符串中大括号之间的字符
import 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)
-----------------------------------------------------------------------------
text = re.sub(r'{[^{}]*}', '', s) # 去除包含在{}中的内容
print('text',text)
-----------------------------------------------------------------------------
sub_text=r'\{.*?\}'
pattern=re.compile(sub_text)
result=re.sub(pattern, '', s).strip()
print()
参考:https://segmentfault.com/q/1010000000655403
https://blog.csdn.net/wenxingchen/article/details/140059412