python – regular expressions for arbitrary objects


Suppose I have a list of some different types of objects, and I want to search this list using regular expressions.

Here's what I roughly want to get:
Class definitions:

class C:
    def __init__(self, number):
        self.number = number

    def __eq__(self, other):
        if isinstance(other, type(self)):
            return self.number == other.number
        return False
    def __repr__(self):
        return '{}({})'.format(self.__class__.__name__, self.number)

#Тут для простоты классы (Tf, Ts, O) являются наследниками C,
#но в реальной жизни это может быть и не так

class Tf(C):
class Ts(C):
class O(C):

re.match((type(C), type(C))) on [C(1), C(2), Tf(1), Ts(5)] returns [C(1), C(2)] (as in the template, 2 objects of type C .)

  1. re.match((type(C), type(C))) on [Tf(1), Ts(5)] returns [] (empty since there are no objects of type C)

  2. re.match((C(1), type(Tf))) to [O(1), C(1), Tf(2)] returns [C(1), Tf(2)] (one object of type C by direct match and one object of type Tf by type match)

  3. re.match((*type(C))) on [C(1), C(2)] returns [C(1), C(2)] (that is, as in regulars, I was given all objects of type C )

Is there an engine somewhere that allows this? (Preferably in python, but another language will work as well). I could not find..

PS: I didn't seem to make myself very clear. Look, we have a regular like this \d{2} it gives us 2 numbers from a string. I want exactly the same thing, but to take out not numbers, but the types or objects that I indicated in the template, and take it out not from the string, but from the list. That is, regular expressions are searched for by symbols, but I need to search for all types in general.


If I understood correctly, how is it?

l = ['12int3sfd', 'wefwestr111', '!typedsfdf', '!floatdfdf']
r = re.compile("type|float")
for i in filter(, l):
    ...:     print(i)
Scroll to Top