[1018]python去除特定字符串之间的内容

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周小董

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值