你@"\[\[([^)]*)\]\]"
模式匹配两个连续[[
,接着以比其他)
零个或多个字符,然后,接着有两个]]
。这意味着,如果您在[[...]]
内有)
,则不会有匹配。
要处理多字符分隔的子字符串,可以使用2种方法:延迟点匹配或展开的模式。
注意:获得多个匹配,使用Regex.Matches
正如我在其他的答案中写道。
1.懒惰点溶液:
var s = "User name [[sales]] and [[anotherthing]]";
var matches = Regex.Matches(s, @"\[{2}(.*?)]{2}", RegexOptions.Singleline)
.Cast<Match>()
.Select(p => p.Groups[1].Value)
.ToList();
见regex demo。 .
需要RegexOptions.Singleline
修饰符才能匹配换行符号。
2.展开正则表达式溶液:
var s = "User name [[sales]] and [[anotherthing]]";
var matches = Regex.Matches(s, @"\[{2}([^]]*(?:](?!])[^]]*)*)]{2}")
.Cast<Match>()
.Select(p => p.Groups[1].Value)
.ToList();
有了这一个,RegexOptions.Singleline
不是必需的,并且它是更加有效和更快。
见regex demo
使用'Regex.Matches'。 –
我投票结束这个问题作为离题,因为这可以解决检查intellisense建议或只是检查什么方法'正则表达式'对象支持MSDN。 –
我更新了它,以更准确地解决我的问题。 Wiktor的回答原来的问题,但事实证明,没有为我的实际情况这么好。 – naspinski