我有一个形式为的正则表达式
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方言(可能不是Python的方言)中使用,而不是特定于Python的解决方案。