Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

Python regex map multiple matches to one

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If this is a puzzle and you really want a single re.search statement:

import regex

result = regex.search('(?<=.*(dog|cat|bird).*)animal$', 'a bag of birdseed' + 'animal')
print(result[0])

re only allows a fixed width lookbehind in standard Python, so you have to use regex (which is a slightly more powerful common regex engine for Python.

The reason this works is because you just add 'animal' to the subject and you match only that word at the end of the string, if it is preceded by any of the animal words.

However, since those are of varying length, re won't do.

If this isn't a puzzle, you're better off with:

import re

result = re.search('dog|cat|bird', 'a bag of birdseed')
if result:
    print('animal')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...