python中findall函数怎么调用

   2025-02-15 6570
核心提示:在Python中,可以使用re模块中的findall()函数来查找字符串中的所有匹配项。语法:re.findall(pattern, string, flags=0)参数说

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

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

参数说明:

pattern: 匹配的正则表达式string: 要匹配的字符串flags: 可选参数,用于控制正则表达式的匹配方式,默认为0表示不使用任何标志

示例代码:

import retext = "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.commary@example.com

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

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言