Regular expressions commonly used in development

In order to better understand how to use regular expressions in the C ා environment, here are some common regular expressions:

Roman number:

string p1 = "^m*(d?c{0,3}|c[dm])" + "(l?x{0,3}|x[lc])(v?i{0,3}|i[vx])$";
string t1 = "v";
Match m1 = Regex.Match(t1, p1);

Exchange the first two word positions:

string t2 = "the quick brown fox"; 
string p2 = @"(\S+)(\s+)(\S+)"; 
Regex x2 = new Regex(p2); 
string r2 = x2.Replace(t2, "$3$2$1", 1);

Key word = value:

string t3 = "myval = 3";
string p3 = @"(\w+)\s*=\s*(.*)\s*$";
Match m3 = Regex.Match(t3, p3);

Implement 80 characters per line:

string t4 = "********************" 
              + "******************************" 
              + "******************************"; 
string p4 = ".{80,}"; 
Match m4 = Regex.Match(t4, p4);

Month / day / year hour: minute: second time format:

string t5 = "01/01/01 16:10:01"; 
string p5 = @"(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)"; 
Match m5 = Regex.Match(t5, p5);

Change directory (Windows platforms only):

string t6 = @"C:\Documents and Settings\user1\Desktop\"; 
string r6 = Regex.Replace(t6,@"\\user1\\", @"\\user2\\");

Extended 16 bit escape character:

string t7 = "%41"; // capital A 
string p7 = "%([0-9A-Fa-f][0-9A-Fa-f])"; 
string r7 = Regex.Replace(t7, p7, HexConvert);

Delete comments in C (to be improved):

string t8 = @"    
    /*    
     * Traditional style notes    
     */    
    "; 
   
    string p8 = @"    
     /\* # Match comment start delimiter    
     .*? # Match notes    
     \*/ # Match comment end delimiter    
    ";    
    string r8 = Regex.Replace(t8, p8, "", "xs");

To remove spaces at the beginning and end of a string:

string t9a = " leading"; 
string p9a = @"^\s+"; 
string r9a = Regex.Replace(t9a, p9a, ""); 
string t9b = "trailing "; 
string p9b = @"\s+$"; 
string r9b = Regex.Replace(t9b, p9b, "");

After the character \, add the character n to make it a real new line:

string t10 = @"\ntest\n"; 
string r10 = Regex.Replace(t10, @"\\n", "\n");

Translate 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 = Regex.Match(t11, p11);

Delete the path that the filename contains:

string t12 = @"c:\file.txt"; 
string p12 = @"^.*\\"; 
string r12 = Regex.Replace(t12, p12, "");

Join rows in a multiline string:

string t13 = @"this is    
    a split line";    
string p13 = @"\s*\r?\n\s*";    
string r13 = Regex.Replace(t13, p13, " ");

Extract all numbers in the string:

string t14 = @"    
    test 1    
    test 2.3    
    test 47    
    "; 
string p14 = @"(\d+\.?\d*|\.\d+)"; 
MatchCollection mc14 = Regex.Matches(t14, p14);

Find all capital letters:

string t15 = "This IS a Test OF ALL Caps"; 
string p15 = @"(\b[^\Wa-z0-9_]+\b)"; 
MatchCollection mc15 = Regex.Matches(t15, p15);

Find the lowercase words:

string t16 = "This is A Test of lowercase"; 
string p16 = @"(\b[^\WA-Z0-9_]+\b)"; 
MatchCollection mc16 = Regex.Matches(t16, p16);

Find the word with the first letter uppercase:

string t17 = "This is A Test of Initial Caps"; 
string p17 = @"(\b[^\Wa-z0-9_][^\WA-Z0-9_]*\b)"; 
MatchCollection mc17 = Regex.Matches(t17, p17);

Find links in a simple HTML language:

string t18 = @"    
    <html>    
    <a href=""first.htm"">first tag text</a>    
    <a href=""next.htm"">next tag text</a>    
    </html> 
       "; 
string p18 = @"<A[^>]*?HREF\s*=\s*[""']?" + @"([^'"" >]+?)[ '""]?>"; 
MatchCollection mc18 = Regex.Matches(t18, p18, "si");

This article comes from Muzhuang online blog> Regular expressions commonly used in development

Keywords: C# P4 Windows

Added by jdsflash on Mon, 21 Oct 2019 18:46:32 +0300