我有一个python列表,我想要一个正则表达式来删除包含至少5个大写字母的子字符串。另一个正则表达式可以删除字符串的一部分从' ?' till ': '
INPUT : list = [‘helLo/aPPle/BuTTeRfLY:Missed’,’bliss/ScIENCEs/brew?Dyna=skjdk:Nest’,’Self/NESTeDsd/hello/MiSSInG:Good’]
Output : list = [‘helLo/aPPle/:Missed’,’bliss//brew:Nest’,’Self//hello/:Good’]
这里制作2个regex:
- (w*[A-Z]w*){5,} -查找至少5个大写字母
- ?.*(?=:) -查找以?开头的子字符串并以: 结尾
-
如果我们发现字符串与正则表达式模式匹配,则将字符串替换为"并更新列表中的值
import re reg =r'(w*[A-Z]w*){5,}|?.*(?=:)' input_list = ["helLo/aPPle/BuTTeRfLY:Missed","bliss/ScIENCEs/brew?Dyna=skjdk:Nest","Self/NESTeDsd/hello/MiSSInG:Good"] for data in input_list: match = re.finditer(reg,data) if match: for match_word in match: print(match_word) if match_word.group() in data: # if uppercase char >5 then replace this substring with '' final_str = data.replace(str(match_word.group()),'') # find index of data index = input_list.index(data) # replce new value in list input_list[index] = data =final_str print(input_list)
输出:: -("你好/苹果/:错过了","幸福//啤酒:巢","自我//helLo/:好']