Thursday 22 July 2021

Is it possible to write Python regexp with something like AND operator?

I can't find a nice way to write several regexp into one such that input string is checked against all sub-regexps like this:

def match(input_str: str, regexp: str) -> bool:
    ...

print(match('abaaca', '.*aba.*<AND>.*aca.*'))  # True
print(match('abaca', '.*aba.*<AND>.*aca.*'))  # True, it doesn't matter that one letter a is shared
print(match('abac', '.*aba.*<AND>.*aca.*'). # False

Is there any way to do it better than parsing regexp to see if there is <AND> in it, split the string into several sub-regexps and match in cycle?

UPD: to be clear, I am looking for a way to use it as a full-featured operator, in cases like ((a<AND>b)|(c<AND>d))<AND>e which will match all of the strings abe, bae, cde and dce. Not only one <AND> but several, mixed with parentheses.



from Is it possible to write Python regexp with something like AND operator?

No comments:

Post a Comment