现在的位置: 首页 > 综合 > 正文

NET正则表达式问号等特殊字符的转义与按组名分组引用

2012年04月26日 ⁄ 综合 ⁄ 共 760字 ⁄ 字号 评论关闭
 

正则表达式中问号等特殊字符的转义
在.NET Framework 开发人员指南 的 字符转义 一节 有这样一段话。
除 .$ ^ { [ ( | ) * + ? \ 外,其他字符与自身匹配。
但是其中并未注明这些字符应该匹配为什么字符。
为了方便自己以后查阅,也为了方便网友搜索我就写在这里了。
[Sipo]
点的转义:.  ==> \\u002E
美元符号的转义:$  ==> \\u0024
乘方符号的转义:^  ==> \\u005E
左大括号的转义:{  ==> \\u007B
左方括号的转义:[  ==> \\u005B
左圆括号的转义:(  ==> \\u0028
竖线的转义:| ==> \\u007C
右圆括号的转义:) ==> \\u0029
星号的转义:*  ==> \\u002A
加号的转义:+  ==> \\u002B
问号的转义:?  ==> \\u003F
反斜杠的转义:\ ==> \\u005C

按组名分组引用

The following code example uses named grouping constructs to capture substrings from a string containing data in a "DATANAME:VALUE" format that the regular expression splits at the colon (:).

    Regex r = new Regex("^(?<name>\\w+):(?<value>\\w+)");
    Match m = r.Match("Section1:119900");

This regular expression returns the following output

m.Groups["name"].Value = "Section1"
m.Groups["value"].Value = "119900"

抱歉!评论已关闭.