问题
So I have...
regex = re.compile('\d{4,}:\s##(.*)##')
regex_search = regex.search(line.mesg)
# This code is abbreviated. I go on to do things with
# "result" in the try block but rather than junk up this
# post, here's the gist:
try:
result = regex_search.group(1)
except AttributeError:
print regex_search
Sample 'line.mesg' input (4 separate instances of line.mesg):
##Loading task.##
##BEEP (375,2)##
##Aisle One One Seven##;pp
##Good night.##
Now, according to the testing at pythex.org (click to see), I should be getting results from this. Yet every time I run through that code, it gets an exception, and print regex_search prints 'None'.
I'm not really sure what's wrong here, any help would be appreciated.
EDIT: I'm a moron - what I was testing with and what I was actually putting in weren't the same thing. The above regex does work for what I originally had for input (and what is found at the above pythex link) but what I was actually throwing into regex.search was what is currently above in the second code block.
TL;DR: unsurprisingly, when you rely on digits to match your regex, and then those digits aren't there, the regex doesn't match.
回答1:
Ah i see the issue, if you are not tied to using search you can utilise re.findall like in the example below.
import re
line = '''(5/12/14 10:22:36 AM EDT) 34438: ##Loading task.##
(5/12/14 10:22:52 AM EDT) 3094962: ##BEEP (375,2)##
(5/12/14 10:22:52 AM EDT) 3095975: ##Aisle One One Seven##;pp
(5/12/14 10:40:07 AM EDT) 4132712: ##Good night.##'''
regex = re.compile('\d{4,}:\s##(.*)##+')
regex_search = regex.findall(line)
try:
for result in regex_search.groups():
print result
except AttributeError:
print regex_search
which returns
['Loading task.', 'BEEP (375,2)', 'Aisle One One Seven', 'Good night.']
回答2:
What seems to be incorrect:
- The result should be printed in the try block.
- There is an extra ":" in the except line.
I don't know the exact input format of your line.mesg, so it is possible something is wrong there. But the following code works:
import re
line = '''\
(5/12/14 10:22:36 AM EDT) 34438: ##Loading task.##
(5/12/14 10:22:52 AM EDT) 3094962: ##BEEP (375,2)##
(5/12/14 10:22:52 AM EDT) 3095975: ##Aisle One One Seven##;pp
(5/12/14 10:40:07 AM EDT) 4132712: ##Good night.##
'''
regex = re.compile('\d{4,}:\s##(.*)##')
for line_msg in line.split('\n'):
regex_search = regex.search(line_msg)
try:
result = regex_search.group(1)
print result # result should be printed here
except AttributeError: # remove the ":" after "except"
print regex_search
来源:https://stackoverflow.com/questions/24492398/python-regex-search-giving-none-when-it-should-match