Skip to content

Commit 53f7214

Browse files
Merge pull request geekcomputers#508 from sethiojas/find_phoneNumbers_and_email
Find phone numbers and email
2 parents 10ff93d + 3c30659 commit 53f7214

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

about.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# phone_and_email_regex
2+
Searches for phone numbers (india only) and email IDs from the most recent text in clipboard.
3+
Displays the number of matches and stores all the matches(both numbers and email IDs) in 'matches.txt'

ph_email.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/python3
2+
3+
# find phone numbers and email addresses
4+
#./ph_email.py searches for phone numbers and emails in the latest clipboard
5+
#entry and writes the matches into matches.txt
6+
7+
import pyperclip
8+
import re
9+
10+
#Phone regex overview per line
11+
#word boundary
12+
#area code +91, 91, 0
13+
#optional space
14+
#ten numbers
15+
#word boundary
16+
17+
find_phone = re.compile(r'''\b
18+
(\+?91|0)?
19+
\ ?
20+
(\d{10})
21+
\b
22+
''', re.X)
23+
24+
25+
#email regex source : http://www.regexlib.com/REDetails.aspx?regexp_id=26
26+
find_email = re.compile(r'''(
27+
([a-zA-Z0-9_\-\.]+)
28+
@
29+
((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)
30+
|
31+
(([a-zA-Z0-9\-]+\.)+))
32+
([a-zA-Z]{2,4}|[0-9]{1,3})
33+
(\]?)
34+
)
35+
''', re.X)
36+
37+
text = pyperclip.paste() #retrieve text from clipboard
38+
39+
matches = [] #list to store numbers and emails
40+
41+
#ph[1] means second item of the group-wise tuple
42+
#which is returned by findall function
43+
#same applies to email
44+
45+
for ph in find_phone.findall(text):
46+
matches.append(ph[1])
47+
48+
for em in find_email.findall(text):
49+
matches.append(em[0])
50+
51+
#display number of matches
52+
print(f"{len(matches)} matches found")
53+
54+
#if matches are found add then to file
55+
if len(matches):
56+
with open('matches.txt', 'a') as file:
57+
for match in matches:
58+
file.write(match)
59+
file.write('\n')

0 commit comments

Comments
 (0)