Learning Regex looking for criticism

Joined
Jul 20, 2023
Messages
57
Reaction score
2
Man I'm surprised how difficult of a time I had learning this module. I got into it because I bought the udemy "Automate the boring stuff with python" course. Although having the physical book probably would've been much better, I must say regardless other tutorials on youtube were much easier to follow. But anyway I digress. I just tried making my own challenges and god it took me a while to get it right. But now that I did get it to work, I would like to see if there is a more efficient way to do it or just different way. I'm very sure there are several. Please let me know what you think.
Python:
import re

# find and extract all area codes that are reasonably cipherable as an area code
message = "801-111-1111,802-222-2222, (803)-333-3333,(804) 444-4444, -805-555-5555, 806 666-6666"
pattern_object = r'\(\d\d\d\)|\d\d\d(?=-\d\d\d-)|\d\d\d(?= \d\d\d-)'

checked = re.compile(pattern_object)
area_codes = checked.findall(message)
# found_obj = checked.search(message)

# print(area_codes)
for area_code in area_codes:
    print(area_code)

Furthermore it would be nice if I could see a version where numbers are grouped by names "area code", "prefix", and "line". And then substitute the last two digits with "XX"
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top