A good Regular Expression in C #

Source: Internet
Author: User

To better understand how to use regular expressions in the C # environment, I have written some regular expressions that may be useful to you. These expressions have been used in other environments, hope to 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 = Regex. Match (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 = x2.Replace (t2, "$3 $2 $1", 1 );
Guan jianzi = 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 + )";
Match m5 = Regex. Match (t5, p5 );
Change directory (applicable only to Windows)
String t6 = @ "C: \ Documents ents and Settings \ user1 \ Desktop \";
String r6 = Regex. Replace (t6, @ "\ user1 \", @ "\ user2 \\");
Extended 16-bit escape characters
String t7 = "% 41"; // capital
String p7 = "% ([0-9A-Fa-f] [0-9A-Fa-f])";
String r7 = Regex. Replace (t7, p7, HexConvert );
Delete comments in C Language (to be improved)
String t8 = @"
/*
* Notes for traditional styles
*/
";
String p8 = @"
/\ * # Match the delimiters starting with the Annotation
.*? # Matching comments
\ */# End Separator of matching Annotation
";
String r8 = Regex. Replace (t8, p8, "", "xs ");
Deletes spaces at the start 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 ,"");
Add character n after character \ to make it a real New Line
String t10 = @ "\ ntest \ n ";
String r10 = Regex. Replace (t10, @ "\ n", "\ n ");
Convert IP addresses
String t11 = "55.54.53.52 ";
String p11 = "^" +
@ "([01]? \ D | 2 [0-4] \ d | 25 [0-5]) \. "+
@ "([01]? \ D | 2 [0-4] \ d | 25 [0-5]) \. "+
@ "([01]? \ D | 2 [0-4] \ d | 25 [0-5]) \. "+
@ "([01]? \ D | 2 [0-4] \ d | 25 [0-5]) "+
"$ ";
Match m11 = Regex. Match (t11, p11 );
Delete the path contained in the file name
String t12 = @ "c: \ file.txt ";
String p12 = @ "^ .*\\";
String r12 = Regex. Replace (t12, p12 ,"");
Concatenate rows from multiple strings
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 1, 2.3
Test 47
";
String p14 = @ "(\ d + \.? \ D * | \. \ d + )";
MatchCollection mc14 = Regex. Matches (t14, p14 );
Find all uppercase letters
String t15 = "This IS a Test of all Caps ";
String p15 = @ "(\ B [^ \ Wa-z0-9 _] + \ B )";
MatchCollection mc15 = Regex. Matches (t15, p15 );
Find 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 words whose first letter is 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 ");

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.