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