Help with Regular Expressions

Hey all,

I am trying to create a simple program that finds dates listed in text, and then prints those dates out. This is just for practice, as I am very new to python and coding. I am getting an error that says "sre_constants.error: missing ), unterminated subpattern at position 56 (line 6, column 5)"

Could someone take a look at my code an tell me what I did wrong? Thanks.

import re

import pyperclip

Month_Detector = re.compile('''r (
    (\d\d)
    (/|.|\s)
    (\d\d)
    (/|.|\s)
    ((\d\d\)|(\d\d\d\d\))
    )''', re.VERBOSE)
    
text = Month_Detector.findall('There was an incident on 01/31/1994. Then there was another on 01.31.2005')

print(str(text))

Posted Mar 19, 2020, 4:00 PM UTC

110
lifeeraser•

You're using triple-quoted raw strings the wrong way. Start your pattern with

r'''

instead of

'''r

Also, the dot is actually a special character in a regular expression that matches any character. To match an exact dot, use \. instead of .

Example:

(/|\.|\s)

Finally, it seems you have accidentally escaped some closing parentheses. Look for and replace \) with ).

1
TheTeachingMirrorOP•

Thanks for the edits. I made some of those changes, but now the error is

Traceback (most recent call last):

File "c:\users\acer\mu_code\regex test.py", line 9, in <module>

)''', re.VERBOSE)

File "re.py", line 233, in compile

File "re.py", line 301, in _compile

File "sre_compile.py", line 562, in compile

File "sre_parse.py", line 869, in parse

sre_constants.error: unbalanced parenthesis at position 81 (line 7, column 5)

​

Here is my updated code:

import re

Month_Detector = re.compile(r'''(
    (\d\d)
    (/|\.|\s)
    (\d\d)
    (/|\.|\s)
    ((\d\d)|\(\d\d\d\d))
    )''', re.VERBOSE)
    
text = Month_Detector.findall('There was an incident on 01/31/1994. Then there was another on 01.31.2005')

print(str(text))
1
lifeeraser•

You also have an accidentally escaped opening parenthesis (\() here:

((\d\d)|\(\d\d\d\d))

It should be

((\d\d)|(\d\d\d\d))

or, for simplicity, just omit the inner parentheses which don't serve any useful purpose:

(\d\d|\d\d\d\d)
1
TheTeachingMirrorOP•

Ah - great. Thank you!. Okay, now it is giving me the output, but not the output I wanted. Here is my code: import re

Month_Detector = re.compile(r'''(
    (\d\d)+
    (/|\.|\s)+
    (\d\d)+
    (/|\.|\s)+
    ((\d\d)|(\d\d\d\d))
    )''', re.VERBOSE)
    
text = Month_Detector.findall('There was an incident on 01/31/1994. Then there was another on 01.31.2005')


print(text)    

And the output that I get is

        [('01/31/19', '01', '/', '31', '/', '19', '19', ''), ('01.31.20', '01', '.', '31', '.', '20', '20', '')]

However, what I really wanted was ['01/31/19', '01.31.20']. Is there a way to edit the code to do that? Thanks again for the help!

1
lifeeraser•

findall() returns a list of "matches". In this case, you have two "matches", each "match" being a tuple:

  • First match: ('01/31/19', '01', '/', '31', '/', '19', '19', '')
  • Second match: ('01.31.20', '01', '.', '31', '.', '20', '20', '')

Since you are only interested in the entire string of each "match", pick the first item in each tuple and store them in a new list:

matches = Month_Detector.findall('There was an incident on 01/31/1994. Then there was another on 01.31.2005')
dates = [match[0] for match in matches]
print(dates)
1