re.findall()函数使用

re.findall(pattern, string, flags=0)

返回string中所有非重叠的匹配——字符串列表

Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.


使用:

url_str = """{

        'keyword=AAAAAAE5%9C%9F1&select',

        'keyword=E5%A3%A4%E6%9C0&select',

        'keyword=%E6%9C%BA%E6%A2&select',

        'keyword=4%E6%88%C%BA%E6&select',

        }"""

pattern = re.compile(r"keyword=([^&]+)&select")

matches = re.findall(pattern, url_str)

# 下面一行效果同上面两行

# matches = re.compile(r"keyword=([^&]+)&select").findall(url_str)

结果:['AAAAAAE5%9C%9F1', 'E5%A3%A4%E6%9C0', '%E6%9C%BA%E6%A2', '4%E6%88%C%BA%E6']

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容