1 Star 0 Fork 0

dzc/Python-1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ph_email.py 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
Ojas Sethi 提交于 2019-07-17 23:38 +08:00 . uses regex to find email and numbers
#!/usr/bin/python3
# find phone numbers and email addresses
#./ph_email.py searches for phone numbers and emails in the latest clipboard
#entry and writes the matches into matches.txt
import pyperclip
import re
#Phone regex overview per line
#word boundary
#area code +91, 91, 0
#optional space
#ten numbers
#word boundary
find_phone = re.compile(r'''\b
(\+?91|0)?
\ ?
(\d{10})
\b
''', re.X)
#email regex source : http://www.regexlib.com/REDetails.aspx?regexp_id=26
find_email = re.compile(r'''(
([a-zA-Z0-9_\-\.]+)
@
((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)
|
(([a-zA-Z0-9\-]+\.)+))
([a-zA-Z]{2,4}|[0-9]{1,3})
(\]?)
)
''', re.X)
text = pyperclip.paste() #retrieve text from clipboard
matches = [] #list to store numbers and emails
#ph[1] means second item of the group-wise tuple
#which is returned by findall function
#same applies to email
for ph in find_phone.findall(text):
matches.append(ph[1])
for em in find_email.findall(text):
matches.append(em[0])
#display number of matches
print(f"{len(matches)} matches found")
#if matches are found add then to file
if len(matches):
with open('matches.txt', 'a') as file:
for match in matches:
file.write(match)
file.write('\n')
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sdredboy/Python-1.git
git@gitee.com:sdredboy/Python-1.git
sdredboy
Python-1
Python-1
master

搜索帮助