简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM