forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathph_email.py
executable file
·65 lines (52 loc) · 1.34 KB
/
ph_email.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/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 re
import pyperclip
# 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")