Is there an equivalent emxs' rx macro in python?

The rx macro in emacs (see http://www.emacswiki.org/emacs/rx and http://doc.endlessparentheses.com/Fun/rx ) allows you to define regular expressions in a modular and readable way (at least you don’t need to take care of problems with quotes). For example:

(rx "a" (optional "c") "b")

leads to

"ac?b"

Is there anything comparable in python?

+4
source share
1 answer

IMO, LISP RegEx, . ? RegEx ; ; rx . Omnimark? . ...

, :

def optional(regex):
    return regex + "?"


def regex(*args):
    return "".join(args)


REGEX = regex("a", optional("b"), "c")
print(REGEX)

:

ab?c
0

Source: https://habr.com/ru/post/1624508/


All Articles