I am working on a program with regexes, I have to filter them but I can't find out how. I want to match every red,xxxx or xxxx,red expression in my string and put the colors xxxx into a group. Here is my code:
string = "blue,red red,yellow blue,yellow red,green purple red, ..."
regex = re.开发者_运维知识库compile('(?:red,(?P<redfirst>\w+)|(?P<othercolorfirst>\w+),red)')
Then I write:
for match in regex.finditer(string):
if match.group('redfirst')!= "None":
print(match.group("redfirst"))
But I still obtain printing like:
None
yellow
green
None
I dont want the 'None' results to appear, I have to skip them in an smart way if possible. Thanks for help!
EDIT None without quotes doesn't work either
>>> import re
>>> regex = re.compile('(?:red,(?P<redfirst>\w+)|(?P<othercolorfirst>\w+),red)')
>>> string = "blue,red red,yellow blue,yellow red,green purple red, ..."
>>> for matches in regex.finditer(string):
... if matches.group('redfirst'):
... print matches.group('redfirst')
...
yellow
green
>>>
The result when nothing matches isn't "None"
(the string), it's None
(the singleton object). And while simply stripping the quotes around None
in your condition works, it's preferred to use ... is None
for numerous reasons, the most important being that it's in the style guide (hey, consistency wins - usually) and that it doesn't break on a badly-written __eq__
(not an issue here and more of a paranoia anyway, but since there are no downsides, why not?).
I would suggest something like this:
>>> redfirst, othercolorfirst = zip(*(m.groups() for m in regex.finditer(string)))
>>> redfirst
(None, 'yellow', 'green')
>>> othercolorfirst
('blue', None, None)
>>> filter(None, redfirst)
('yellow', 'green')
>>> filter(None, othercolorfirst)
('blue',)
>>> print "\n".join(filter(None, redfirst))
yellow
green
精彩评论