python

python中findall函数怎么调用

小亿
86
2023-11-04 16:47:06
栏目: 编程语言

在Python中,可以使用re模块中的findall()函数来查找字符串中的所有匹配项。

语法: re.findall(pattern, string, flags=0)

参数说明:

示例代码:

import re

text = "Hello, my name is John. My email address is john@example.com. My friend's email is mary@example.com."
emails = re.findall(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', text)

for email in emails:
    print(email)

输出结果:

john@example.com
mary@example.com

在上面的示例中,使用了正则表达式来匹配字符串中的电子邮件地址。re.findall()函数返回一个列表,包含了所有匹配到的电子邮件地址。然后使用for循环遍历列表,并打印每个电子邮件地址。

0
看了该问题的人还看了