SoFunction
Updated on 2025-03-02

C# Regular Expression Classic Classification Collection Manual Page 1/3


string i = "Live for nothing,die for something";
Regex r1 = new Regex("^Live for nothing,die for something$");
//(i) true
Regex r2 = new Regex("^Live for nothing,die for some$");
//(i) false
Regex r3 = new Regex("^Live for nothing,die for some");
//(i) true

string i = @"Live for nothing,
die for something";//Multiple lines
Regex r1 = new Regex("^Live for nothing,die for something$");
("r1 match count:" + (i).Count);//0
Regex r2 = new Regex("^Live for nothing,die for something$", );
("r2 match count:" + (i).Count);//0
Regex r3 = new Regex("^Live for nothing,\r\ndie for something$");
("r3 match count:" + (i).Count);//1
Regex r4 = new Regex("^Live for nothing,$");
("r4 match count:" + (i).Count);//0
Regex r5 = new Regex("^Live for nothing,$", );
("r5 match count:" + (i).Count);//0
Regex r6 = new Regex("^Live for nothing,\r\n$");
("r6 match count:" + (i).Count);//0
Regex r7 = new Regex("^Live for nothing,\r\n$", );
("r7 match count:" + (i).Count);//0
Regex r8 = new Regex("^Live for nothing,\r$");
("r8 match count:" + (i).Count);//0
Regex r9 = new Regex("^Live for nothing,\r$", );
("r9 match count:" + (i).Count);//1
Regex r10 = new Regex("^die for something$");
("r10 match count:" + (i).Count);//0
Regex r11 = new Regex("^die for something$", );
("r11 match count:" + (i).Count);//1
Regex r12 = new Regex("^");
("r12 match count:" + (i).Count);//1
Regex r13 = new Regex("$");
("r13 match count:" + (i).Count);//1
Regex r14 = new Regex("^", );
("r14 match count:" + (i).Count);//2
Regex r15 = new Regex("$", );
("r15 match count:" + (i).Count);//2
Regex r16 = new Regex("^Live for nothing,\r$\n^die for something$", );
("r16 match count:" + (i).Count);//1
//For a multiline string, after setting the Multiline option, ^ and $ will match multiple times.

string i = "Live for nothing,die for something";
string m = "Live for nothing,die for some thing";
Regex r1 = new Regex(@"\bthing\b");
("r1 match count:" + (i).Count);//0
Regex r2 = new Regex(@"thing\b");
("r2 match count:" + (i).Count);//2
Regex r3 = new Regex(@"\bthing\b");
("r3 match count:" + (m).Count);//1
Regex r4 = new Regex(@"\bfor something\b");
("r4 match count:" + (i).Count);//1
//\b is usually used to constrain a complete word