File tree Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Original file line number Diff line number Diff line change
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'
Original file line number Diff line number Diff line change
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 ' )
You can’t perform that action at this time.
0 commit comments