Nice regular Expressions _ regular expressions in C #

Source: Internet
Author: User
Tags lowercase
To better understand how to use regular expressions in a C # environment, I write some regular expressions that might be useful to you, and these expressions are used in other contexts, hoping 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);
Swap 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);
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 hours: minutes: Seconds of 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 platform only)
string T6 = @ "C:\Documents and settings\user1\desktop\";
string r6 = Regex.Replace (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 = Regex.Replace (T7, P7, Hexconvert);
Delete comments in C language (to be perfected)
string T8 = @ "
/*
* Traditional style annotation
*/
";
String P8 = @ "
/\* # matches the delimiter at the beginning of the comment
.*? # matching annotations
\*/# matching Comment End delimiter
";
String r8 = Regex.Replace (T8, P8, "", "XS");
Deletes a space 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, "");
Add the character n after the character \ to make it a true new line
string T10 = @ "\ntest\n";
string R10 = Regex.Replace (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 = Regex.match (T11, p11);
Delete the path that the file name contains
String T12 = @ "C:\file.txt";
String p12 = @ "^.*\\";
String R12 = Regex.Replace (T12, P12, "");
Join rows in multiple-line strings
string t13 = @ "This is
A split line ";
string p13 = @ "\s*\r?\n\s*";
String R13 = Regex.Replace (t13, P13, "");
Extracts all numbers in a string
string t14 = @ "
Test 1
Test 2.3
Test 47
";
String p14 = @ "(\d+\.? \d*|\.\d+) ";
MatchCollection mc14 = regex.matches (t14, p14);
Find all the Caps
String t15 = "This is a Test of all Caps";
String p15 = @ "(\b[^\wa-z0-9_]+\b)";
MatchCollection mc15 = regex.matches (t15, P15);
Find a lowercase word
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 as 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 a link in a simple HTML language
String t18 = @ "
<a href= "" first.htm "" >first tag text</a>
<a href= "" next.htm "" >next tag text</a>
";
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.