C # getting started with regular expressions

Source: Internet
Author: User

C # (asp.net) Regular Expressions are very important in programming and development, and are easy to ignore for some programmers. This is mainly for commonly used c # (asp.net) regular Expressions can be found online. But for a Senior Programmer, this must be mastered. The method for understanding c # (asp.net) regular expressions is actually simple. We only need to familiarize ourselves with its syntax and remember several common c # (asp.net) regular expressions.

Common syntax in c # (asp.net) Regular Expressions: character matching, repeat matching, character locating, escape matching, character grouping, character replacement, and character decision-making. For Quick Start users, remember: character matching syntax, repeat matching syntax, character locating syntax, and escape matching syntax. The detailed documents of these four syntaxes are as follows.

Character matching syntax

Character syntax Syntax explanation Syntax example
\ D Matching number (0 ~ 9) '\ D' matches 8 and does not match 12;
\ D Match non-Numbers '\ D' matches c and does not match 3;
\ W Match any single character '\ W \ W' matches A3 and does not match @ 3;
\ W Match non-single character '\ W' matches @ and does not match c;
\ S Match blank characters '\ D \ s \ d' matches 3 d and does not match abc;
\ S Match non-null characters '\ S \ s' matches A #4 and does not match 3 d;
. Match any character '...' Matches A $5 and does not match the line feed;
[…] Match any character in brackets [B-d] matches B, c, and d, but does not match e;
[^…] Match non-parentheses [^ B-z] matches a and does not match B-z characters;

Repeat matching syntax

Repeated syntax Syntax explanation Syntax example
{N} Match n characters \ D {3} matches \ d, does not match \ d or \ d
{N ,} Match n times or more \ W {2} matched \ w and \ w or above, not matched \ w
{N, m} Match n times above m times \ S {1, 3} matches \ s, and \ s does not match
? Match 0 or 1 time 5? Match 5 or 0, not 5 or 0
+ Match once or multiple times \ S + matches more than one \ S and does not match more than one \ S
* Match more than 0 times \ W * matches 0 and above \ W, but does not match non-N * \ W

Character locating syntax

Repeated syntax Syntax explanation Syntax example
^ Locate the start position of the subsequent mode  
$ The front mode is at the end of the string  
\ Start position of the previous Mode  
\ Z End position of the previous Mode  
\ Z End position of the previous mode (before line feed)  
\ B Match A Word boundary  
\ B Match a non-word boundary  

Escape matching syntax

Syntax Involved characters (syntax explanation) Syntax example
"\" + Actual characters \. * +? | () {}^ $ For example, \ matches the character "\"
\ N Match line feed  
\ R Match carriage return  
\ T Match horizontal tabs  
\ V Match vertical tabs  
\ F Match form feed  
\ Nnn Matches an octal ASCII  
\ Xnn Matches a hexadecimal ASCII  
\ Unnnn Match 4 hexadecimal Uniode  
\ C + uppercase letters Match Ctrl-UPPERCASE letters Example: \ cS-match Ctrl + S

Constructing Regular Expressions involves the Regex class. The Regex classes include IsMatch (), Replace (), Split (), and Match;
IsMatch (): Match
Replace (): Replace
Split (): Split

The c # (asp.net) Regular Expression Regex class uses an instance:
Example 1: Determine the phone number in Changsha (using IsMatch ())
Analysis: the telephone number in Changsha is a 0731 area code followed by eight digits. Regular Expression write Rule: 0731 \ d {8}
Code:
String Regextest = "0731 \ d {8 }";
String testphone = "07323452343343 ";
If (Regex. IsMatch (testphone, Regextest ))
{
Response. write ("the correct Changsha region number! ");
}
Else
{
Response. write ("Incorrect Changsha region number! ");
}

Example 2: Replace the character @ in the text with AT (Replace ())
Analysis: first, determine the mail format in the text, and then replace @ with AT in the mail. The regular expression write rule is: Determine the mail format: "http://www.cnblogs.com/ceoliujia/admin/file://w{1,#@//w{1 ,}//.";
Code:
String Regextest = "\ w {1 ,}\\@\\ w {1 ,}\\.";
String Email = "lanqilin's Email is 19085107@qq.com ";
If (Regex. Ismath (Email, Regextest ))
{
Regex. Replace (Email, "@", "");
}

Example 3: Read all email addresses in all mass emails (use Split ())
Analysis: one email address per line is used in the text stored in group mail. \ r \ n is used as the delimiter and the score must be discounted by \ r \ n.
Code:
String RegexTest = "\ r \ n ";
String allemail = "19085107@qq.com \ r \ n860184649@qq.com ";
String [] singemail = Regex. Split (allemail, RegexTest );
Foreach (string str in singemail)
{
Response. Write (str. Tostring ());
}

Examples of common c # (asp.net) Regular Expressions:
Verification number: "^ [0-9] * $"
Verification letter: "^ [A-Za-z] + $ ".
Verification Email: "^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) * $"
Verify Chinese characters: "^ [\ u4e00-\ u9fa5] {0,} $"

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.