简体   繁体   English

C#正则表达式模式这与之匹配

[英]C# Regular expression pattern what this matches for

能有人解释这个正则表达式将检查什么

Regex x = new Regex("{([^}]+)}");

It looks for a {...} with some (1 or more) non-} inside. 它寻找一个{...} ,其中包含一些(1个或多个) non-} If successful it puts the content of the {...} in capture group 1. 如果成功,它将{...}的内容放入捕获组1。

Regex x = new Regex("{([^}]+)}");
var m = x.Match("{Hello}");

string str0 = m.Groups[0].ToString(); // {Hello}
string str1 = m.Groups[1].ToString(); // Hello

Group 0 is always the whole match. 组0始终是整个比赛。

var m2 = x.Match("{}");
var success = m2.Success; // false

It isn't anchored, so it could have more than one match for each string... 它不是固定的,因此每个字符串可能有多个匹配项...

var m2 = x.Matches("{Hello}{}{World}");
int c = m2.Count; // 2 matches. The {} wasn't a match, {Hello} and {World} were

As a sidenote, if you think this is the beginning for a good C# parser, you are on the wrong road :-) Expressions like { { string str = "Hello"; } str += "x"; } 附带说明,如果您认为这是一个好的C#解析器的开始,那么您走的路不对:-)像{ { string str = "Hello"; } str += "x"; } { { string str = "Hello"; } str += "x"; } { { string str = "Hello"; } str += "x"; } will confuse this regex, so expressions like { string str = "}" } . { { string str = "Hello"; } str += "x"; }会混淆此正则表达式,因此类似{ string str = "}" }表达式。 This is a stackless regex. 这是一个无堆栈的正则表达式。 No fancy tricks. 没有花哨的花招。

It matches anything between curly braces, if there at least one character. 如果有至少一个字符,则它匹配花括号之间的所有字符。

There is group () inside braces {} . 大括号{}有group () This group should have at least one []+ symbol which is not closing brace ^} . 该组中至少应有一个[]+符号,而不是右括号^}

它匹配花括号之间的任何内容,例如{ddhhh13233dddd} {ddd}

This will catch everything within the curly brackets. 这将抓住大括号内的所有内容。

This MSDN article can explain it in more detail. MSDN文章可以对其进行更详细的说明。

This will catch everything within the curly brackets. 这将抓住大括号内的所有内容。

I find this tool is the best for explaining Regex's 我发现此工具是解释Regex的最佳工具

http://tinyurl.com/lz3d458 http://tinyurl.com/lz3d458

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM