private String pattern = @"^{(.*)|(.*)}$";
ret = groups[0].Captures.Count.ToString(); // returns 1
Run Code Online (Sandbox Code Playgroud)
是不是应该返回2次捕获?因为()我的RegExp中有两个?
我的字符串例如:
{test1 | test2}
Run Code Online (Sandbox Code Playgroud)
第一次捕获应该是test1和secnd test2,但我得到整个字符串作为回报,捕获计数是1为什么是这样?
更新:
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches(_sourceString);
String ret = "";
foreach (Match match in matches)
{
GroupCollection groups = match.Groups;
ret = groups[0].Captures[0].Value;
}
return ret; //returns the whole string, but I've expected 'test1'
Run Code Online (Sandbox Code Playgroud)
|在正则表达式中有特殊含义.A|B比赛A或B.
要按|字面意思匹配,你需要逃避它:
@"^{(.*)\|(.*)}$";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51 次 |
| 最近记录: |