我有与Python Reverse Find in String先前相似的问题。
这里是我很长的字符串的示例:
t1 = '''1281674 the crescent annandale 02/10/2019 16/10/2019 - 16/11/2019 pending 1281640 city west link rd lilyfield 02/10/2019 16/10/2019 - 16/11/2019 pending 1276160 victoria rd rozelle 25/09/2019 14/10/2019 - 15/10/2019 pending 1331626 31/12/2019 - 31/01/2020 incomplete n/a 1281674 the crescent annandale 02/10/2019 16/10/2019 - 16/11/2019'''
更新:2020年1月2日
我将数据分组到列表中,然后放入数据框。我不需要与'incomplete n/a'
相关的任何数据,是否需要删除字符串或是否存在正则表达式函数来识别'incomplete n/a'
并在其位置上分组?
我想要两个输出:
ONE此列表t1L = ['1281674 ', '1281640 ', '1276160 ']
。请注意,这不包括1331626。
TWO例如,要拆分或重新定义的字符串(不包含1331626):
t1 = '''1281674 the crescent annandale 02/10/2019 16/10/2019 - 16/11/2019 pending 1281640 city west link rd lilyfield 02/10/2019 16/10/2019 - 16/11/2019 pending 1276160 victoria rd rozelle 25/09/2019 14/10/2019 - 15/10/2019 pending'''
感谢您的帮助。
我认为您的问题new_str = t1[:t1.find(re.findall('\d{7}', t1[:t1.find('incomplete n/a')])[-1])])
有可用的代码
import re
t1 = '''1281674 the crescent annandale 02/10/2019 16/10/2019 - 16/11/2019 pending 1281640 city west link rd lilyfield 02/10/2019 16/10/2019 - 16/11/2019 pending 1276160 victoria rd rozelle 25/09/2019 14/10/2019 - 15/10/2019 pending 1331626 31/12/2019 - 31/01/2020 incomplete n/a 1314832'''
res = re.search(r'(\b\d{7}\b)(?=(?:(?!\b\d{7}\b).)*incomplete n/a)', t1)
print res.group(1)
输出:
1331626
您可以使用循环和条件尝试以下代码。
import re
t1 = '1281674 the crescent annandale 02/10/2019 16/10/2019 - 16/11/2019 pending 1281640 city west link rd lilyfield 02/10/2019 16/10/2019 - 16/11/2019 pending 1276160 victoria rd rozelle 25/09/2019 14/10/2019 - 15/10/2019 pending 1331626 31/12/2019 - 31/01/2020 incomplete n/a 1314832 '
result = None
for t in t1.split(" "):
if re.match("\d{7}",t):
result = t
if 'incomplete' in t:
break
print(result)