I can’t find how to proceed for a regular expression, here is an example:
string = "red\\/banana 36 monkey\\/apple 14 red\\/apple 23 red\\/horse 56 bull\\/red 67 monkey\\/red 45 bull\\/shark 89"
I want to do a single regex with re.match.group() which will take into account only the ones like red/xxxx and the ones like xxxx/red and group the xxxx names only, not couples:
I want to do:
print(match.group("beginningwithred") + " " + match.group("number")
and obtain:
banana 36
apple 23
horse 56
then do:
print(match.group("endingwithred") + " " + match.group("number")
and obtain:
bull 67
monkey 45
my current code goes like:
iterator = regex.finditer(string)
for match in iterator:
regex = re.compile('red\\\\\\\\/(?P<beginningwithred>banana|apple|horse)|(?P<endingwithred>bull|monkey)\\\\\\\\/red (?P<number>\d\d)')
but it doesn’t work, I can’t use | between groups and python HOWTO doesn’t help..
I tried with { } too including the whole two expressions but it doesn’t work either.
It must not be really complicated but I can’t find out what’s wrong.
i don’t completely follow, but it sounds like you want non-capturing groups around your alternatives:
that lets you use
|without creating a “real” group.update why doesn’t this help? is this not right?
update2
if you just want to print out the non-
Nonevalues you can do something like: