python - 在 python 中评论正则表达式

标签 python regex

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/

相关文章:

python - 使用 numpy 和 matplotlib 进行 PCA 标注的困难

javascript - 使用正则表达式验证输入中是否包含任何非数字

java - 用正则表达式替换特定标识符

python - 删除列表中的列表

python - 合并时忽略空数据框

python正则表达式查找列表中字符串的所有第一个起始单词

regex - 在 Notepad++ 中删除重复的字符串

php - 使用先前的反向引用作为命名捕获组的名称

python - 如何在Python中对版本标签列表进行排序

python - 类型错误 : __init__() missing 2 required positional arguments: 'client_socket' and 'statusMessage'