我从头开始学习正则表达式,并且已经在这个正则表达式问题上挣扎了一段时间,但仍然没有解决它。
输入:
exception_lst = ['r&b', 'm&a']
str_lst = ['fe r&b fe', 'sar&bd', 'sd r &B b', 'as r& b ac', 'sdf O&B sd', 'df R&B sdc', 's r&p es', 'sf m & A df']
预期输出:
str_lst = ['fe r&b fe', 'sar and bd', 'sd r&b b', 'as r&b ac', 'sdf O and B sd', 'df R&B sdc', 's r and p es', 'sf m&a df']
我想要实现的是
我在这里尝试了几个,但没有一个满足所有要求。
我的正则表达式(和Python知识)也很有限,但我尝试过,但没有这个似乎有效:
import re
exception_lst = ['r&b', 'm&a']
str_lst = ['fe r&b fe', 'sar&bd', 'sd r &B b', 'as r& b ac', 'sdf O&B sd', 'df R&B sdc', 's r&p es', 'sf m & A df']
print("START:")
print(str_lst)
str_lst_new = []
for i in str_lst:
#print(i)
InExceptionLst = False
for j in exception_lst:
if re.search(' '+j+' ',i,re.IGNORECASE):
InExceptionLst = True
if not InExceptionLst:
m1 = re.search(r'([a-zA-Z0-9]*r)&(b[a-zA-Z0-9]*)', i, re.IGNORECASE)
m2 = re.search(r'([a-zA-Z0-9]*m)&(a[a-zA-Z0-9]*)', i, re.IGNORECASE)
if m1 or m2 or not(m1 and m2):
#print(i.replace('&',' and '))
str_lst_new += [i.replace('&',' and ')]
else:
#print(i)
str_lst_new += [i]
else:
str_lst_new += [i]
print("END:")
print(str_lst_new)
输出:
START:
['fe r&b fe', 'sar&bd', 'sd r &B b', 'as r& b ac', 'sdf O&B sd', 'df R&B sdc', 's r&p es', 'sf m & A df']
END:
['fe r&b fe', 'sar and bd', 'sd r and B b', 'as r and b ac', 'sdf O and B sd', 'df R&B sdc', 's r and p es', 'sf m and A df']```