gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:53:38 25 4
gpt4 key购买 nike

在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com