I'd like a regex to return 'animal' if a string contains for instance 'dog', 'cat' or 'bird'.
I can find them easily using: re.search('(dog|cat|bird)', mystring)
but that will return 'dog', 'cat' or 'bird', for instance:
result = re.search('(dog|cat|bird)', 'a bag of birdseed')
print(result)
<_sre.SRE_Match object; span=(9, 13), match='bird'>
print(result[0])
bird
Is it possible to craft a regex that will return 'animal' for any of the options?
So for the above example:
result = re.search(magic_regex, 'a bag of birdseed')
print(result[0])
animal
I am limited to re.search
(existing, fixed code), the only freedom I have is the regex.
The code that does the matching is inside a library (which I don't want to change unless I really have to). The library code is used to parse the response (using regexes) of (serial) commands we send to an embedded device. The responses are then mapped or converted to human readable values and displayed in a GUI-based configuration tool. Users can edit the values in the tool and write them back, without having to know any of the intricacies of the serial commands.
We keep the regexes for all commands for each device in a database (a set of .ini files, containing hundreds of commands for several devices and their different firmware versions). The library code that does the matching takes the regex and the response of a command and returns the matching data (if any).
The library code is now generic and can handle any type of response, but it can't 'map' multiple matching strings to one value, hence my question.
If I can resolve my issue with a 'magic regex' then I don't have to touch the library. If that is not possible, I will have to look for a different solution, possibly changing the library code.
question from:
https://stackoverflow.com/questions/65927710/python-regex-map-multiple-matches-to-one 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…