我有一个形式的正则表达式
def parse(self, format_string):
for m in re.finditer(
r"""(?: \$ \( ( [^)]+ ) \) ) # the field access specifier
| (
(?:
\n | . (?= \$ \( ) # any one single character before the '$('
)
| (?:
\n | . (?! \$ \( ) # any one single character, except the one before the '$('
)*
)""",
format_string,
re.VERBOSE):
...
我想\$ \(
用一些自定义的速记“常量”替换所有重复序列 ( ),如下所示:
def parse(self, format_string):
re.<something>('\BEGIN = \$\(')
for m in re.finditer(
r"""(?: \BEGIN ( [^)]+ ) \) ) # the field access specifier
| (
(?:
\n | . (?= \BEGIN ) # any one single character before the '$('
)
| (?:
\n | . (?! \BEGIN ) # any one single character, except the one before the '$('
)*
)""",
format_string,
re.VERBOSE):
...
有没有办法用正则表达式本身来做到这一点(即不使用 Python 的字符串格式来替换\BEGIN
)\$\(
?
澄清: Python 源代码纯粹是为了上下文和说明。我正在寻找 RE 解决方案,它可以在某些 RE 方言中使用(可能不是 Python 的方言),而不是专门针对 Python 的解决方案。