1

I'm only a month into python and this basic exercise is driving me up the wall. I'm trying to make a search script that will search text I input and put the results on my clipboard. I've been stuck below for about a week. If i copy text directly from a site and input it, no results (I get a None output). But if i copy in the number directly, no problems it reads them perfectly. I've tried it several ways (shown below) and no luck, it has me stumped.Below is a sample text I paste in that gives me no results:

‌Dr. Someone Spam

Room: BLD-2001

+353 (0)11 123456

Any input people can provide would be great. Also a side question, any books/advice on learning python would be amazing. Currently following "Automate the boring stuff with python". Just doing it for fun. Thanks in advance.

import re, pyperclip

def findphone(numbers):
    numregex = re.compile(r'\(\d\)\d\d\s\d+')
    numregex1 = re.compile(r'(0)11 123456')
    phoneRegex = re.compile(r'''(
    (\+\d{3})?                    # area code
    (\s|-|\.)?                    # separator
    (\d{3}|\(\d\)\d\d)?           # area code
    (\s|-|\.)?                    # separator
    \d{6}                         # Landline Number 
    )''', re.VERBOSE)
    mo = numregex.search(numbers)
    mo0 = numregex1.search(numbers)
    mo1 = phoneRegex.search(numbers)
    print('mo ' +str(mo))
    print('mo0 ' +str(mo0))
    print('mo1 ' +str(mo1))

print('Input check text')
numbers = input()
findphone(numbers)
2
  • I don't understand the difference between "copy text directly from a site and input it" vs "copy in the number directly." Also, you might find this useful: regex101.com/#python Commented Jun 8, 2016 at 1:45
  • 1
    Sorry John, didn't explain myself properly in that line. The goal of the exercise was to input a large string, sort it, and output the results to my clipboard using pyperclip. Alan below put me on good tracks. I'm deconstructing his edits in that regex link you sent me to fully get my head around it. Thanks a million! Commented Jun 8, 2016 at 13:04

1 Answer 1

1

See changes inline

# -*- coding: utf-8 -*-
# 1. added above line
import re

def findphone(numbers):
    numregex = re.compile(r'(\(\d\)\d\d\s\d+)') # 2. Added circular brackets around the regular expression
    numregex1 = re.compile(r'(\(0\)11 123456)') # 3. Escaped circular brackets around 0

    # 4. Made small changes to the following, mainly changing brackets.
    phoneRegex = re.compile(r'''
    (\+\d{3})                    # area code
    [\s|-|\.]                     # separator
    (\d{3}|\(\d\)\d\d)?           # area code
    [\s|-|\.]                     # separator
    (\d{6})                         # Landline Number 
    ''', re.VERBOSE)
    mo = numregex.search(numbers)
    mo0 = numregex1.search(numbers)
    mo1 = phoneRegex.search(numbers)
    if mo:
      print('mo ' +str(mo.groups()))
    if mo0:
      print('mo0 ' +str(mo0.groups()))
    if mo1:
      # 5. break down in separate variables
      country_code = mo1.groups()[0]
      area_code = mo1.groups()[1]
      landline = mo1.groups()[2]
      print country_code, area_code, landline

print('Input check text')
findphone("‌Dr. Someone Spam\nRoom: BLD-2001\n+353 (0)11 123456")
Sign up to request clarification or add additional context in comments.

2 Comments

Switching to a character class is good, but get rid of the pipes: [\s.-]. Also, you can use group() to return the whole match, and group(1), group(2) etc. to return the groups. Much neater that what you're doing with groups().
Thank you Alan, I appreciate the help and the detailed breakdown of the edits you made, huge help to a beginner like myself!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.