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