How to realize alternative matching in C #
This article mainly introduces "how C#realizes one-choice matching". In daily operation, I believe many people have doubts about how C#realizes one-choice matching. Xiao Bian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how C#realizes one-choice matching"! Next, please follow the small series to learn together!
C#regular expression (|There seems to be no special name for the symbol. Let's call it "alternative matching." In fact, something like [a-z] is also an alternative match, except that it can only match a single character, and (|It provides a wider range,(ab| xy) means matching ab or matching xy. Attention."|"And"()"is a whole here.
Here are some simple C#regular expression alternative matching examples:
string x = "0"; string y = "0.23"; string z = "100"; string a = "100.01"; string b = "9.9"; string c = "99.9"; string d = "99. "; string e = "00.1"; Regex r = new Regex(@"^\+? ((100(.0+)*)|([1-9]? [0-9])(\.\ d+)*)$"); Console.WriteLine("x match count:" + r.Matches(x).Count);//1 Console.WriteLine("y match count:" + r.Matches(y).Count);//1 Console.WriteLine("z match count:" + r.Matches(z).Count);//1 Console.WriteLine("a match count:" + r.Matches(a).Count);//0 Console.WriteLine("b match count:" + r.Matches(b).Count);//1 Console.WriteLine("c match count:" + r.Matches(c).Count);//1 Console.WriteLine("d match count:" + r.Matches(d).Count);//0 Console.WriteLine("e match count:" + r.Matches(e).Count);//0 //matches a number from 0 to 100. The outermost parenthesis contains two parts "(100(.0+)*)," //"([1-9]? [0-9])(\.\ d+)*", //These two parts are "OR" relations, //i.e. the regular expression engine tries to match 100 first, //If it fails, try matching the latter expression (representing a number in the range [0,100)). At this point, the study of "how to achieve alternative matching of C#" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!