新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

python中findall函数如何调用,python find find_all

发布时间:2023-11-04 16:47:06

python中findall函数如何调用

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

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

参数说明:

  • 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'[A-Za-z0⑼._%+-]+@[A-Za-z0⑼.-]+.[A-Z|a-z]{2,}', text)

for email in emails:
    print(email)

输出结果:

john@example.com
mary@example.com

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