获取python列表

时间:2016-07-27 22:06:31

标签: python

如何从列表中获取字符串&quotautoRefresh之间的内容/commander/link/jobDetails/jobs/a2537f238-8622-11ee-a1a0-f0921c14c828?,如下所示,我只需要第一个匹配项(可能有多个匹配项)。< / p>

['something', 'something', ' something top.window.location.href = &quot;/commander/link/jobDetails/jobs/a2537f238-8622-11ee-a1a0-f0921c14c828?autoRefresh=0&amp;s=Jobs&quot;;">','something']

尝试

link = re.search('&quot;(.*?)autoRefresh', big_list)
print link.group(1)

得到TypeError: expected string or buffer

2 个答案:

答案 0 :(得分:0)

您需要遍历列表,检查每个字符串:

big_list = ['something', 'something', ' something top.window.location.href = &quot;/commander/link/jobDetails/jobs/a2537f238-8622-11ee-a1a0-f0921c14c828?autoRefresh=0&amp;s=Jobs&quot;;">','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, '&quot;(.*?)autoRefresh', 1)))

或者在列表中拨打str.join并使用 findall

print(re.findall('&quot;(.*?)autoRefresh', "".join(big_list)))

答案 1 :(得分:0)

您可以使用以下内容:

re.search(r'(?<=&quot).*?(?=autoRefresh)', ''.join(YourList))