我在 lst
中有一个字符串列表lst = ["-", "/", ","]
str to re.sub:
的示例str = "abc - abc 9-4 " => "abc abc 9-4 "
str = "abc , abc 9/4 " => "abc abc 9,4 "
str = "abc / abc 9,4 " => "abc abc 9/4 "
我想用字符串replace_with
替换给定模式的所有匹配项我想按如下方式实现它
new_str = re.sub(pattern,replace_with, str);
其中,
replace_with = ""
pattern = Please help me define the following in regex
(不是数字)(任何东西)( lst 中的任何字符串)(任何东西)(不是) 数)
答案 0 :(得分:1)
你可以试试这个:
lst = ["-", "/", ","]
import re
s = ["abc - abc 9-4 ", "abc , abc 9/4 ", "abc / abc 9,4 "]
final_data = [re.sub('\s|\s'.join(lst), ' ', i) for i in s]
输出:
['abc abc 9-4 ', 'abc abc 9/4 ', 'abc abc 9,4 ']