C# Regex "Verbose"就像在 Python 中一样

标签 c# python regex

在 Python 中,我们有 re.VERBOSE 参数,它允许我们很好地格式化 regex 表达式并包含注释,例如

import re

ric_index = re.compile(r'''(
    ^(?P<delim>\.)    # delimiter "."
    (?P<root>\w+)$    # Root Symbol, at least 1 character
    )''',re.VERBOSE)

C#中有类似的东西吗?

最佳答案

您可以使用逐字字符串(使用@),它允许您编写:

var regex = new Regex(@"^(?<delim>\\.)     # delimiter "".""
                    (?<root>\\w+)$    # Root Symbol, at least 1 character
                    ", RegexOptions.IgnorePatternWhitespace);

注意 RegexOptions.IgnorePatternWhitespace 的使用编写详细正则表达式的选项。

关于C# Regex "Verbose"就像在 Python 中一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50683833/

相关文章:

c# - 仅内容 Nuget 包无法恢复内容

c# - 如何与微服务共享 Web API Controller

c# - 在 teamcity 中并行运行单元测试?

python - 如何在python中隐藏刻度标签但保持刻度不变?

regex - 使用带有字边界的 mgsub 函数作为替换值

c# - 如何使我的数字生成器包含最大值? C#

python - 如何在Python中使用字典作为字典的键

java - 如何编写正则表达式来查找以下模式?

双引号之间的 JavaScript 文本

python - 导入 tflearn 时出现 "Scipy not supported!"