登录后复制 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) 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23. 原创作者: u_14121041 转载于: https://blog.51cto.com/u_14121041/11411639