C# 正则表达式“详细"就像在 Python 中一样 [英] C# Regex "Verbose" like in Python
本文介绍了C# 正则表达式“详细"就像在 Python 中一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Python 中,我们有 re.VERBOSE
参数,它允许我们很好地格式化 regex
表达式并包含注释,例如
In Python, we have the re.VERBOSE
argument that allows us to nicely format regex
expressions and include comments, such as
import re
ric_index = re.compile(r'''(
^(?P<delim>\.) # delimiter "."
(?P<root>\w+)$ # Root Symbol, at least 1 character
)''',re.VERBOSE)
C# 中是否有类似的东西?
Is there something similar in C#?
推荐答案
您可以使用逐字字符串(使用 @
),它允许您编写:
You can use verbatim string (using @
), which allow you to write:
var regex = new Regex(@"^(?<delim>\\.) # delimiter "".""
(?<root>\\w+)$ # Root Symbol, at least 1 character
", RegexOptions.IgnorePatternWhitespace);
注意使用 RegexOptions.IgnorePatternWhitespace
选项用于编写详细的正则表达式.
Note the use of RegexOptions.IgnorePatternWhitespace
option to write verbose regexes.
这篇关于C# 正则表达式“详细"就像在 Python 中一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文