1

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

3 Answers 3

3
>>> 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
>>>
Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

bad answer... I check my facts before posting, None without quotes doesn't work for me
@Kingpin: That's a strong claim, you'll have to support it with code. None is a builtin and globally available, and group is documented to return None when the group didn't match.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.