user906780
user906780

Reputation:

regex result filter in python

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

Upvotes: 1

Views: 615

Answers (3)

Andrew Clark
Andrew Clark

Reputation: 208465

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

Upvotes: 1

Ben
Ben

Reputation: 52863

>>> 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
>>>

Upvotes: 3

user395760
user395760

Reputation:

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?).

Upvotes: 2

Related Questions