如何用一个正则表达式查找多个匹配项?

时间:2019-10-10 15:10:43

标签: python regex

我有一个像这样的字符串:

to_search = "example <a>first</a> asdqwe <a>second</a>"

我想在这两者之间找到两个解决方案:

list = ["first","second"]

我知道在搜索一种解决方案时,我应该使用以下代码:

import re

if to_search.find("<a>") > -1:
    result = re.search('<a>(.*?)</a>', to_search)
    s = result.group(1)
    print(s)

但是只能打印:

first

我尝试了result.group(2)和result.group(0),但是我得到了相同的解决方案


我如何列出所有解决方案?

3 个答案:

答案 0 :(得分:1)

与正则表达式相比,最好使用HTML解析器,但将import org.apache.commons.collections4.MultiValuedMap; //import org.apache.commons.collections4.MultiValuedMap.ArrayListValuedHashMap; //import org.apache.commons.collections4.map.MultiValueMap; import org.apache.commons.collections4.multimap.*; //import org.apache.commons.collections4.*; //import org.apache.commons.collections4.MultiValuedMap.*; //import org.apache.commons.collections4.map.*; 更改为re.search

答案 1 :(得分:1)

只需使用:

import re
to_search = "example <a>first</a> asdqwe <a>second</a>"
matches = re.findall(r'<a>(.*?)</a>', to_search)
print(matches)

输出

['first', 'second']

答案 2 :(得分:0)

to_search = "example <a>first</a> asdqwe <a>second</a>"
for match in re.finditer("<a>(.*?)</a>", to_search):
    captured_group = match.group(1)
    # do something with captured group
相关问题