C # Regular expression pattern for which it matches

Can someone explain that this regex will check for

Regex x = new Regex("{([^}]+)}"); 
+4
source share
5 answers

He is looking for {...} with some (1 or more) non-} inside. If successful, it places the contents of {...} in capture group 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 always matches everything.

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

It is not bound, so it can have more than one match for each row ...

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

As a side element, if you think this is the beginning for a good C # parser, you are on the wrong track :-) Expressions like { { string str = "Hello"; } str += "x"; } { { string str = "Hello"; } str += "x"; } { { string str = "Hello"; } str += "x"; } will confuse this regular expression, therefore expressions like { string str = "}" } . This is an irregular regular expression. No fancy tricks.

+4
source

It matches anything between curly braces if there is at least one character.

There is a group () inside curly braces {} . This group must have at least one []+ character that does not close the brackets ^} .

+2
source

It matches anything between curly braces for example {ddhhh13233dddd} {ddd}

+1
source

This will cover all curly braces.

This MSDN article article can explain it in more detail.

0
source

This will cover all curly braces.

I find this tool to best explain

Regex's <

http://tinyurl.com/lz3d458

enter image description here

0
source

Source: https://habr.com/ru/post/1495899/


All Articles