Skip to content

Instantly share code, notes, and snippets.

@ShyftXero
Created April 13, 2021 15:57
Show Gist options
  • Save ShyftXero/15f35d3e6f5219a863d9ba3c9cfefaf3 to your computer and use it in GitHub Desktop.
Save ShyftXero/15f35d3e6f5219a863d9ba3c9cfefaf3 to your computer and use it in GitHub Desktop.

Revisions

  1. ShyftXero created this gist Apr 13, 2021.
    39 changes: 39 additions & 0 deletions regex.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #! python3
    # phoneAndEmail.py - Finds phone numbers and email addresses on the clipboard.

    # Site to test against: https://dese.ade.arkansas.gov/Offices/ar-comp-sci-initiative/statewide-computer-science-specialists

    import pyperclip, re

    # Create phone number regex with or without area code, but uses a '-' seperator.
    phoneRegex = re.compile(r''' EXPRESSION HERE ''', re.VERBOSE)

    # Create phone number regex with no seperators, with or without leading 1 before area code.
    phone2Regex = re.compile(r''' EXPRESSION HERE ''', re.VERBOSE)

    # Create email regex.
    emailRegex = re.compile(r''' EXPRESSION HERE ''', re.VERBOSE)

    # Find matches in clipboard text.
    text = str(pyperclip.paste())
    matches = []
    for groups in phoneRegex.findall(text):
    phoneNum = '-'.join([groups[1], groups[3], groups[5]])
    if groups[8] != '':
    phoneNum += ' x' + groups[8]
    matches.append(phoneNum)

    for groups in phone2Regex.findall(text):
    phoneNum = '-'.join([groups[2], groups[3], groups[4]])
    matches.append(phoneNum)

    for groups in emailRegex.findall(text):
    matches.append(groups[0])

    # Copy results to the clipboard
    if len(matches) > 0:
    pyperclip.copy('\n'.join(matches))
    print('Copied to clipboard:')
    print('\n'.join(matches))
    else:
    print('No phone numbers or email addresses found.')