用正則表達式查找字符串中所有的email,並輸出這些email。
- 要求:
- 所有的email域名必須是.com 或.net 的
- 不區分大小寫
# findall函數的用法
# 用於搜索字符串中所有滿足條件的子字符串
# 第一個參數: 用於指定正則表達式
# 第二個參數: 用於指定待匹配的字符串
# 第三個參數: 用於指定選項,如re.I表示忽略大小寫
import re
s = '我的Email是[email protected], 你的email是[email protected]嗎, 還是[email protected]'
prefix = '[0-9a-zA-Z]+@[0-9a-zA-Z]+\.'
result = re.findall(prefix + 'com|' + prefix + 'net', s, re.I)
print(result)
['[email protected]', '[email protected]']