This answer关于正则表达式可维护性的问题提到 .NET 用户在他们的正则表达式中实现注释的能力(我对第二个例子特别感兴趣)
有没有一种简单的 native 方法可以在 python 中重现它,最好不需要安装第三方库或编写我自己的注释条算法?
我目前所做的与该答案中的第一个示例类似,我在多行中连接正则表达式并注释每一行,如以下示例所示:
regexString = '(?:' # Non-capturing group matching the beginning of a comment
regexString += '/\*\*'
regexString += ')'
最佳答案
您正在寻找 VERBOSE re
模块中的标志。其文档中的示例:
a = re.compile(r"""\d + # the integral part
\. # the decimal point
\d * # some fractional digits""", re.X)
关于python - 在 python 中评论正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20668996/