如何从列表中获取字符串"
和autoRefresh
之间的内容/commander/link/jobDetails/jobs/a2537f238-8622-11ee-a1a0-f0921c14c828?
,如下所示,我只需要第一个匹配项(可能有多个匹配项)。< / p>
['something', 'something', ' something top.window.location.href = "/commander/link/jobDetails/jobs/a2537f238-8622-11ee-a1a0-f0921c14c828?autoRefresh=0&s=Jobs";">','something']
尝试
link = re.search('"(.*?)autoRefresh', big_list)
print link.group(1)
得到TypeError: expected string or buffer
答案 0 :(得分:0)
您需要遍历列表,检查每个字符串:
big_list = ['something', 'something', ' something top.window.location.href = "/commander/link/jobDetails/jobs/a2537f238-8622-11ee-a1a0-f0921c14c828?autoRefresh=0&s=Jobs";">','something']
def get_all_subs(lst, pat, grp=0):
patt = re.compile(pat)
for s in lst:
m = patt.search(s, grp)
if m:
yield m.group(grp)
print(list(get_all_subs(big_list, '"(.*?)autoRefresh', 1)))
或者在列表中拨打str.join
并使用 findall :
print(re.findall('"(.*?)autoRefresh', "".join(big_list)))
答案 1 :(得分:0)
您可以使用以下内容:
re.search(r'(?<=").*?(?=autoRefresh)', ''.join(YourList))