(4) Repeat description characters
"Repeat description characters" is one of the places where C# regular expressions are "very good and powerful":
{n} Match the previous character n times
{n,} Match the previous characters n times or more
{n,m} Match the previous characters n to m times
? Match the previous character 0 or 1 time
+ Match the previous character 1 or more
* Match the previous character 0 times or 0 times
Here are some simple examples:
Copy the codeThe code is as follows:
string x = "1024";
string y = "+1024";
string z = "1,024";
string a = "1";
string b="-1024";
string c = "10000";
Regex r = new Regex(@"^\+?[1-9],?\d{3}$");
("x match count:" + (x).Count);//1
("y match count:" + (y).Count);//1
("z match count:" + (z).Count);//1
("a match count:" + (a).Count);//0
("b match count:" + (b).Count);//0
("c match count:" + (c).Count);//0
//Match integers from 1000 to 9999.
(5) Choose a match
There seems to be no special title for the (|) symbol in C# regular expression, so let's call it "choose a match". In fact, like [a-z] is also a kind of match selection, except that it can only match a single character, while (|) provides a larger range, (ab|xy) means matching ab or matching xy. Note that "|" and "()" are a whole. Here are some simple examples:
Copy the codeThe code is as follows:
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+)*)$");
("x match count:" + (x).Count);//1
("y match count:" + (y).Count);//1
("z match count:" + (z).Count);//1
("a match count:" + (a).Count);//0
("b match count:" + (b).Count);//1
("c match count:" + (c).Count);//1
("d match count:" + (d).Count);//0
("e match count:" + (e).Count);//0
//Match numbers from 0 to 100. The outermost bracket contains two parts "(100(.0+)*)" and "([1-9]?[0-9])(\.\d+)*". These two parts are a relationship of "OR", that is, the regular expression engine will first try to match 100, and if it fails, try to match the latter expression (represents the number in the range [0,100)).
(6) Matching of special characters
Here are some simple examples:
view plaincopy to clipboardprint?
string x = "\\";
Regex r1 = new Regex("^\\\\$");
("r1 match count:" + (x).Count);//1
Regex r2 = new Regex(@"^\\$");
("r2 match count:" + (x).Count);//1
Regex r3 = new Regex("^\\$");
("r3 match count:" + (x).Count);//0
//match"\"
string x = "\"";
Regex r1 = new Regex("^\"$");
("r1 match count:" + (x).Count);//1
Regex r2 = new Regex(@"^""$");
("r2 match count:" + (x).Count);//1
//Match double quotes
string x = "\\";
Regex r1 = new Regex("^\\\\$");
("r1 match count:" + (x).Count);//1
Regex r2 = new Regex(@"^\\$");
("r2 match count:" + (x).Count);//1
Regex r3 = new Regex("^\\$");
("r3 match count:" + (x).Count);//0
//match"\"
string x = "\"";
Regex r1 = new Regex("^\"$");
("r1 match count:" + (x).Count);//1
Regex r2 = new Regex(@"^""$");
("r2 match count:" + (x).Count);//1
//Match double quotes
(7) Groups and non-capturing groups
Here are some simple examples:
Copy the codeThe code is as follows:
string x = "Live for nothing,die for something";
string y = "Live for nothing,die for somebody";
Regex r = new Regex(@"^Live ([a-z]{3}) no([a-z]{5}),die \1 some\2$");
("x match count:" + (x).Count);//1
("y match count:" + (y).Count);//0
//The regular expression engine will remember the matching content in "()" as a "group" and can be referenced through indexing. "\1" in the expression is used to backreference the first group that appears in the expression, that is, the first bracket content of the bolded text, and "\2" and so on.
string x = "Live for nothing,die for something";
Regex r = new Regex(@"^Live for no([a-z]{5}),die for some\1$");
if ((x))
{
("group1 value:" + (x).Groups[1].Value);//Output: thing
}
//Get the content in the group. Note that here is Groups[1], because Groups[0] is the entire matching string, i.e. the content of the entire variable x.
string x = "Live for nothing,die for something";
Regex r = new Regex(@"^Live for no(?<g1>[a-z]{5}),die for some\1$");
if ((x))
{
("group1 value:" + (x).Groups["g1"].Value);//Output: thing
}
//You can index according to the group name. Use the following format as the name that identifies a group (?<groupname>…).
string x = "Live for nothing nothing";
Regex r = new Regex(@"([a-z]+) \1");
if ((x))
{
x = (x, "$1");
("var x:" + x);//Output: Live for nothing
}
//Delete the repeated "nothing" in the original string. Outside the expression, use "$1" to refer to the first group, and the following is referenced by the group name:
string x = "Live for nothing nothing";
Regex r = new Regex(@"(?<g1>[a-z]+) \1");
if ((x))
{
x = (x, "${g1}");
("var x:" + x);//Output: Live for nothing
}
string x = "Live for nothing";
Regex r = new Regex(@"^Live for no(?:[a-z]{5})$");
if ((x))
{
("group1 value:" + (x).Groups[1].Value);//Output: (empty)
}
//Putting "?:" before the group means that this is a "non-capture group", that is, the engine will not save the contents of the group.
Previous page123Next pageRead the full text