Python 如何用正则表达式替换字符串列表中的字符串? 我的列表=[ '', “先进微设备公司加利福尼亚州桑尼维尔和西德西门子公司” 他说,他们同意共同开发生产和销售微芯片 “以数据通信和电信为重点” “综合业务数字网络综合业务数字网络” “网络或isdn是用于传输语音数据的国际标准” “电话线上的图形和视频图像”, '', “在您5月21日关于手机行业向客户计费的故事中” '未连接的电话我很惊讶您没有讨论是否存在这种情况' “对于保持线路畅通的来电者,计费是合适的” '等待连接使用通信交换和传输' “设备就像正在进行对话一样”, '' ]
我必须用空格替换所有的Python 如何用正则表达式替换字符串列表中的字符串? 我的列表=[ '', “先进微设备公司加利福尼亚州桑尼维尔和西德西门子公司” 他说,他们同意共同开发生产和销售微芯片 “以数据通信和电信为重点” “综合业务数字网络综合业务数字网络” “网络或isdn是用于传输语音数据的国际标准” “电话线上的图形和视频图像”, '', “在您5月21日关于手机行业向客户计费的故事中” '未连接的电话我很惊讶您没有讨论是否存在这种情况' “对于保持线路畅通的来电者,计费是合适的” '等待连接使用通信交换和传输' “设备就像正在进行对话一样”, '' ],python,regex,list,Python,Regex,List,我必须用空格替换所有的(任何变体),并将它们全部添加到自己的列表中。我已经想出了如何使用regex将它们添加到自己的列表中,如下所示: my list = [ '<instance id="line-nw8_059:8174:">', ' advanced micro devices inc sunnyvale calif and siemens ag of west germany ' 'said they agreed to jointly devel
(任何变体),并将它们全部添加到自己的列表中。我已经想出了如何使用regex将它们添加到自己的列表中,如下所示:
my list = [
'<instance id="line-nw8_059:8174:">',
' advanced micro devices inc sunnyvale calif and siemens ag of west germany '
'said they agreed to jointly develop manufacture and market microchips for '
'data communications and telecommunications with an emphasis on the '
'integrated services digital network the integrated services digital '
'network or isdn is an international standard used to transmit voice data '
'graphics and video images over telephone <head>line</head> ',
'<instance id="line-nw7_098:12684:">',
' in your may 21 story about the phone industry billing customers for '
'unconnected calls i was surprised that you did not discuss whether such '
'billing is appropriate a caller who keeps a <head>line</head> open '
'waiting for a connection uses communications switching and transmission '
'equipment just as if a conversation were taking place ',
'<instance id="line-nw8_106:13309:">'
]
instanceList=[]
instanceMatch='您可以使用带替换的正则表达式将所有实例替换为空白。然后,您可以向其传递一个自定义函数,以返回匹配项并将结果附加到实例列表中
for i in contentsTestSplit:
if i.startswith("<instance id="):
i.replace(i,"")
def_sub(匹配):
instanceList.append(匹配[0])
返回“”
instanceList=[]
instanceMatch='我想知道为什么id=“([^”]”)表示字符串,而如果id=“[^”]”则不表示任何内容。什么事
()的使用。它的任何发生/变化都会像这样被拆分成字符串吗?如果是这样的话,那么你就不能简单地做If i.startswith(“这可以识别它们的位置,但我仍然不知道如何替换它?我试过这样做:`` for i in contentststsplit:If i.startswith(“你可以尝试:for i,line in enumerate(contentstsplit:
/If line.startswith(”
instanceList =[]
instanceMatch = '<instance id="([^"]*)"'
pat = re.compile(r'<instance id="([^"]*)"')
for i in contentsTestSplit:
matchy = re.match(instanceMatch,i)
if matchy:
instanceMatchy = matchy.group(0)
instanceList.append(instanceMatchy)
i = pat.sub("",i)
print("instance list: ",instanceList)
for i in contentsTestSplit:
if i.startswith("<instance id="):
i.replace(i,"")
def _sub(match):
instanceList.append(match[0])
return ''
instanceList =[]
instanceMatch = '<instance id="([^"]*)"'
for i in my_list:
re.sub(instanceMatch, _sub, i)