Python, regular expression search matchobj triggers error
-
27-10-2019 - |
Question
I somehow know that the answer to this question will be obvious, but I've spent several days trying unsuccessfully to find out why I don't seem to be able to find the reg.exp. mach object in the script below. Here's the error messsage I get:
subject: Re: Why DOJ BMFEA Baton Rouge rejected Gonzalaz Pen Code 99999 death case
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Traceback (most recent call last):
File "C:\Apps\UtilitiesByMarc\test_search4Sender_aaB.py", line 46, in <module>
print fn_cull_sender_info(date_string_raw)
File "C:\Apps\UtilitiesByMarc\test_search4Sender_aaB.py", line 35, in fn_cull_sender_info
print 'Line 35: matchObj found\n str(match_obj.group(0)) = ' + str(match_obj.group(0))
NameError: global name 'match_obj' is not defined
The following is the code:
import sys
import re, pdb
#pdb.set_trace()
def fn_get_srctxt_hg_datestring_rawdata_from_clipbd():
this_scriptz_FULLName = sys.argv[0]
try:
date_string_raw = sys.argv[1]
return returnval
except:
date_string_raw = ''
import win32clipboard
win32clipboard.OpenClipboard()
clip_text = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
date_string_raw = clip_text
returnval = clip_text
return returnval
def fn_cull_sender_info(date_string_raw): #
# Do re replacements
import re
sender_info = 'Line 29 empty'
print '\n\nLine 30: date_string_raw = [starts on next line...]\n' + str(date_string_raw) + '\n' + 'x'*80 + '\n'
srchpatrn = r"(from:\t )(([A-Za-z\. ]+?)(?: ))?([A-Za-z.\-_0-9]+@[A-Za-z.\-_0-9].+?\.(?:com|org|net))"
matchObj = re.search(srchpatrn, date_string_raw)
if matchObj:
print 'Line 35: matchObj found\n str(match_obj.group(0)) = ' + str(match_obj.group(0))
sender_info = str(match_obj.group(0))
return sender_info
if __name__ == '__main__':
harvey = fn_get_srctxt_hg_datestring_rawdata_from_clipbd()
print harvey
date_string_raw = harvey
print '*****'*50
print '\n\n'
print fn_cull_sender_info(date_string_raw)
And the following (which is text produced by another function [not shown here]) is the text that I have in the Windows Clipboard when I run the python code above:
sender_display_name = matchObj.search(date_string_raw).group(2)#.strip()
sender_eml = matchObj.search(date_string_raw).group(4)
I've spent days trying to find out what I'm doing wrong. I want to capture regular expression groups 3 and 4 into string variables, e.g.
But since I can't get the matchObj to return as True, I can't get there.
Solution
You've previously defined matchObj
, not match_obj
.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow