Python Forum
Automate the boring stuff: regex not matching
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Automate the boring stuff: regex not matching
#1
Hello,
I am trying to do the exercise on regex, matching 3 telephone numbers i a row, with or without area code, with or without comma at the end:

>>> phoneRegex = re.compile(r'((\d\d\d-)?\d\d\d-\d\d\d\d(,)?) {3}')
>>> mo = phoneRegex.search('my numbers are 415-555-1234, 555-4242, 212-555-0000')
This is not matching. And I cannot figure out why.
Reply
#2
If you want to extract phone numbers, you regexp pattern should specify exactly what you want to extract.
You don't need commas, spaces <space>{3}, so why they are included to the pattern .

phoneRegex = re.compile(r'((?:\d\d\d-)?\d\d\d-\d\d\d\d)')
?: added to the first group to prevent grubbing the group as a result.

import re
x = 'my numbers are 415-555-1234, 555-4242, 212-555-0000'
phoneRegex = re.compile(r'((?:\d\d\d-)?\d\d\d-\d\d\d\d)')
phoneRegex.findall(x)
Output:
['415-555-1234', '555-4242', '212-555-0000']
Reply
#3
Hi,
That's not what I need.
I need a regex that returns a result only if I have a group of 3 phone numbers one after the other.
Reply
#4
import re

phone = r'(?:\d{3}-)?\d{3}-\d{4}'
phone3 = r'{p}(?:[\s,]\s*{p}){{2}}\,?'.format(p=phone)

mo = re.search(phone3, 'my numbers are 415-555-1234, 555-4242, 212-555-0000')
print(mo)
Output:
<_sre.SRE_Match object; span=(15, 51), match='415-555-1234, 555-4242, 212-555-0000'>
Reply
#5
Great!
phone3 = r'{p}(?:[\s,]\s*{p}){{2}}\,?'.format(p=phone)
So{p} is phone, your first regex. Can you please explain shortly what are the regex that follow?
Reply
#6
DJ_Qu Wrote:Can you please explain shortly what are the regex that follow?
It's all in the re module's documentation, (?:…){2} is a non capturing group repeated twice. I had to escape the braces because I'm using the format() method, so I write {{2}}. Inside this group, [\s,]\s* means a whitespace character or a comma followed by zero or more whitespace characters, then {p} means another phone number. The \,? at the end means an optional comma.
Reply
#7
Is it me or regex is a headheache??
Thank you for your help!
Reply
#8
Jamie Zawinski Wrote:Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in matching in regex akbarza 1 395 Nov-21-2023, 09:31 AM
Last Post: snippsat
  automate new PDF creation with Bookmarks Based up Regex standenman 0 1,145 Jan-16-2023, 10:56 PM
Last Post: standenman
  Run the code for some stuff it does not return me why Anldra12 3 2,845 Apr-19-2021, 02:01 PM
Last Post: Anldra12
  "Automate the Boring Stuff with Python" creating a path works but only for CMD promt Milos 2 2,864 Nov-28-2020, 01:08 PM
Last Post: Larz60+
  Unable to print stuff from while loop Nick1507 4 2,336 Sep-17-2020, 02:26 PM
Last Post: Nick1507
  Matching Regex in Python from Excelfile and inserting into database Shuzo 0 1,624 May-14-2020, 06:53 PM
Last Post: Shuzo
  How Do I Install Stuff for Python? CopBlaster 6 3,180 May-08-2020, 12:27 PM
Last Post: hussainmujtaba
  Learning to have Class and doing stuff with it... bako 2 1,990 Apr-29-2020, 05:07 PM
Last Post: bako
  Getting Cells from the Sheets "automate the boring stuff" Shafla 8 3,987 Sep-24-2019, 04:53 AM
Last Post: snippsat
  [split] Automate the boring stuff, inserting commas in list srikanth 1 2,107 Jul-02-2019, 02:29 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020