SoFunction
Updated on 2025-04-06

A relatively good regular expression in C#

To better understand how to use regular expressions in C# environment, I wrote some regular expressions that may be useful to you. These expressions have been used in other environments and hope they will help you.
Roman numerals
string p1 = "^m*(d?c{0,3}|c[dm])" + "(l?x{0,3}|x[lc])(v?i{0,3}|i[vx])$"; 
string t1 = "vii"; 
Match m1 = (t1, p1); 
Exchange the first two words
string t2 = "the quick brown fox"; 
string p2 = @"(\S+)(\s+)(\S+)"; 
Regex x2 = new Regex(p2); 
string r2 = (t2, "$3$2$1", 1); 
Keyword = value
string t3 = "myval = 3"; 
string p3 = @"(\w+)\s*=\s*(.*)\s*$"; 
Match m3 = (t3, p3); 
Implement 80 characters per line
string t4 = "********************" 
 + "******************************" 
 + "******************************"; 
string p4 = ".{80,}"; 
Match m4 = (t4, p4); 
Month/Day/Year Hour: Minutes: Second Time Format
string t5 = "01/01/01 16:10:01"; 
string p5 = @"(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)"; 
Match m5 = (t5, p5); 
Change directory (only for Windows platforms)
string t6 = @"C:\Documents and Settings\user1\Desktop\"; 
string r6 = (t6,@"\\user1\\", @"\\user2\\"); 
Extended 16-bit escape characters
string t7 = "%41"; // capital A 
string p7 = "%([0-9A-Fa-f][0-9A-Fa-f])"; 
string r7 = (t7, p7, HexConvert); 
Delete comments in C language (need to be improved)
string t8 = @" 
/* 
* Comments on traditional style
 */ 
"; 
string p8 = @" 
/\*# The delimiter matching the beginning of the comment
.*? # Match comment
\*/ # Match the comment end delimiter
"; 
string r8 = (t8, p8, "", "xs"); 
Delete the spaces at the beginning and end of a string
string t9a = " leading"; 
string p9a = @"^\s+"; 
string r9a = (t9a, p9a, ""); 
string t9b = "trailing "; 
string p9b = @"\s+$"; 
string r9b = (t9b, p9b, ""); 
Add character n after character \ to make it a real new line
string t10 = @"\ntest\n"; 
string r10 = (t10, @"\\n", "\n"); 
Convert IP address
string t11 = "55.54.53.52"; 
string p11 = "^" + 
 @"([01]?\d\d|2[0-4]\d|25[0-5])\." + 
 @"([01]?\d\d|2[0-4]\d|25[0-5])\." + 
 @"([01]?\d\d|2[0-4]\d|25[0-5])\." + 
 @"([01]?\d\d|2[0-4]\d|25[0-5])" + 
 "$"; 
Match m11 = (t11, p11); 
Delete the path contained in the file name
string t12 = @"c:\"; 
string p12 = @"^.*\\"; 
string r12 = (t12, p12, ""); 
Join lines in multi-line strings
string t13 = @"this is 
a split line"; 
string p13 = @"\s*\r?\n\s*"; 
string r13 = (t13, p13, " "); 
Extract all numbers in a string
string t14 = @" 
test 1 
test 2.3 
test 47 
"; 
string p14 = @"(\d+\.?\d*|\.\d+)"; 
MatchCollection mc14 = (t14, p14); 
Find all capital letters
string t15 = "This IS a Test OF ALL Caps"; 
string p15 = @"(\b[^\Wa-z0-9_]+\b)"; 
MatchCollection mc15 = (t15, p15); 
Find lowercase words
string t16 = "This is A Test of lowercase"; 
string p16 = @"(\b[^\WA-Z0-9_]+\b)"; 
MatchCollection mc16 = (t16, p16); 
Find the word whose first letter is capitalized
string t17 = "This is A Test of Initial Caps"; 
string p17 = @"(\b[^\Wa-z0-9_][^\WA-Z0-9_]*\b)"; 
MatchCollection mc17 = (t17, p17); 
Find links in simple HTML language
string t18 = @" 
<html> 
<a href="""">first tag text</a> 
<a href="""">next tag text</a> 
</html> 
"; 
string p18 = @"<A[^>]*?HREF\s*=\s*[""']?" + @"([^'"" >]+?)[ '""]?>"; 
MatchCollection mc18 = (t18, p18, "si");