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 exampleD: matching number (0 ~ 9) 'D' matches 8, does not match 12; d matches non-numeric 'D' matches c, does not match 3; w matches any single character 'ww 'matches A3, do not match @ 3; W match non-single character 'W' match @, do not match c; s match blank character 'dsd' match 3 d, do not match abc; S matches non-null characters 'ss' to match A #4, not 3 d ;. match any character '.... 'matches A $5 and does not match the line feed. […] Match any character in the brackets [B-d] to match B, c, d, not matching e; [^…] Match non-Parentheses [^ B-z]: Match a. do not match B-z;

Repeat matching syntax
Repeated syntax Syntax explanation Syntax example{N} matches n characters, d {3} matches ddd, and dd or dddd {n,} matches n times and n times or more. w {2} matches ww and www, do not match w {n, m} match n times upper m times lower s {1, 3} match s, ss, sss, do not match ssss? Match 0 or 1 time 5? Match 5 or 0, not 5 or 0 + match one or more times S + match more than one S, not matching more than one S * matching more than 0 W * matching more than 0 W, not matching non-N * W

Character locating syntax
Repeated syntax Syntax explanation Syntax example^ Locate the start position in the back mode $ the front mode is located in the string end A. The front mode is located in the start position z. The front mode is located in the end position (before the line feed). Match A Word boundary B to match A non-word boundary.

Escape matching syntax

Syntax Involved characters (syntax explanation) Syntax example"" + Actual characters. * +? | () {}^ $ For example: \ match character "" Match line feed match carriage return match horizontal tab v match vertical tab f match form feed nn match an octal ASCII xnn match a hexadecimal ASCII unnnn match 4 hexadecimal uniode c + uppercase letters match Ctrl-UPPERCASE letters, for 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 = "lanqilins 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, which is used as a separator and must be scored.
Code:
String RegexTest = "";
String allemail = "19085107@qq.com860184649 @ 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,} $"

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.