David
David

Reputation: 15

Regular expression returning None (python)

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)

Upvotes: 1

Views: 502

Answers (1)

Garima Singh
Garima Singh

Reputation: 223

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")

Upvotes: 1

Related Questions