Last active
February 4, 2024 10:58
-
-
Save Navid-JL/e7b9d0f0ab611793cee93867702e7c03 to your computer and use it in GitHub Desktop.
A python script that extracts email addresses and phone numbers from a string in the clipboard memory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Say you have the boring task of finding every phone number and email address in a long web page or document. | |
| # If you manually scroll through the page, you might end up searching for a long time. | |
| # But if you had a program that could search the text in your clipboard for phone numbers and email addresses, you could simply press ctrl-A to select all the text, press ctrl-C to copy it to the clipboard, and then run your program. | |
| # It could replace the text on the clipboard with just the phone numbers and email addresses it finds. | |
| import re | |
| import pyperclip | |
| EMAIL_REGEX = r"([a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)" | |
| PHONE_REGEX = r"\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}" | |
| # Use pyperclip to get text off the clipboard | |
| clipboard_text = pyperclip.paste() | |
| # Find all emails in the text | |
| emails = re.findall(EMAIL_REGEX, clipboard_text) | |
| # Find all phone numbers in the text | |
| phone_numbers = re.findall(PHONE_REGEX, clipboard_text) | |
| # Save emails and phone numbers nicely back to the clipboard | |
| output: str = '' | |
| output += 'Extracted Emails:\n' | |
| for index, email in enumerate(emails): | |
| output += f'{index + 1}- {email}\n' | |
| output += '\nExtracted Phone Numbers:\n' | |
| for index, phone_number in enumerate(phone_numbers): | |
| output += f'{index + 1}- {phone_number}\n' | |
| # Only execute as a script, not as a module | |
| if __name__ == '__main__': | |
| pyperclip.copy(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment