实现正则查找的函数有:

    • re.findall:在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配 的,则返回空列表。
    • re.finditer:在字符串中找到正则表达式所匹配的所有子串,并把它们作为一个迭代器返回。

    看完了正则匹配,相信正则查找对于你来说已经很简单。下面直接举几个例子。
    示例1 我们需要找出这段文本中所有的数字:

    1. s = ' taobao 123 google 456'
    2. re.findall("\d+", s)

    结果:

    1. ['123', '456']

    使用finditer返回迭代器:

    1. it = re.finditer("\d+", s)
    2. for match_obj in it:
    3. print(match_obj.group(), end=" ")

    结果:

    1. 123 456

    示例2
    例如我们希望查找出下面这段英文中所有4个字母的单词:

    1. s = "Clothes are so significant in our daily life that we can't live withoutthem"
    2. re.findall(r"\b[a-z]{4}\b", s, re.I)

    结果:

    1. ['life', 'that', 'live', 'them']

    可以看到很顺利的找到了想要的结果。

    1. \b 表示单词边界,可以回正则规则匹配表查看

    也可以使用re.finditer方法返回一个迭代器:

    1. for match_obj in re.finditer(r"\b[a-z]{4}\b", s, re.I):
    2. print(match_obj.group(), end=" ")

    结果:

    1. life
    2. that
    3. live
    4. them

    注意:re.finditer方法返回的迭代器迭代取出的每一个对象都是 re.Match 对象
    示例3
    提取出下面文本中所有的单词(被双引号引起来的要作为一个单词,例如the little cat,最终结果无需 去重): we found “the little cat” is in the hat, we like “the little cat”

    1. s = 'we found "the little cat" is in the hat, we like "the little cat"'
    2. print(re.findall('\w+|".*?"', s))

    结果:

    1. ['we', 'found', '"the little cat"', 'is', 'in', 'the', 'hat', 'we', 'like','"the little cat"']

    示例4 提取出下面网页中head 标签的内容:

    1. <html>
    2. <head>
    3. <title>学习正则表达式</title>
    4. </head>
    5. <body></body>
    6. </html>

    参考解法:

    1. s = """<html>
    2. <head>
    3. <title>学习正则表达式</title>
    4. </head>
    5. <body></body>
    6. </html>"""
    7. re.findall("(?si)<head>(.*)<\/head>", s)

    结果:

    1. ['\n\t\t<title>学习正则表达式</title>\n\t']