溫馨提示×

python中findall函數怎么調用

小億
141
2023-11-04 16:47:06
欄目: 編程語言

在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'\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
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女