C # Regular Expression Usage Guide

Source: Internet
Author: User

The regular expression is written in English (Regular Expression). According to the use range and Word Meaning of the regular expression,. Net sets its namespace to system. Text. regularexpressions;

Capture Capture results for a single expression
Capturecollection Used to capture strings in a sequence
Group Indicates the result of a single capture.
Groupcollection Indicates a collection of capture groups.
Match Indicates matching results of a single regular expression.
Matchcollection Indicates applying a regular expression to a string through iteration.
RegEx An Immutable regular expression.
Regexcompilationinfo To compile a regular expression, you need to provide information
Basic knowledge of Regular Expressions

Basic syntax

Regular expressions have their own set of syntax rules. Common syntaxes include; character matching, repeat matching, character locating, escape matching, and other advanced syntaxes (character grouping, character replacement, and character decision making );

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;

Repeated 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 Expression

Constructing Regular Expressions involves the RegEx class. The RegEx classes include ismatch (), replace (), split (), and match;

(1) ismatch () method:

The ismatch () method is actually a method that returns bool value. If the test character Meets the regular expression, true is returned; otherwise, false is returned;

Example 1: the phone number of the non-Chengdu region is valid.
Analysis: Chengdu telephone numbers consist of 028 *********. The front is fixed area code 028 and the back is 8 digits;
Design Regular Expression: 028 \ D {8} (Interpretation: 028 area code is fixed, followed by 8 numbers \ D );

(2) Replace () method

The Replace () method is actually a replacement method, replacing the regular expression matching pattern;

Example 2: publish a public email addressArticleReplace @ at to avoid spam;

Analysis: First, you need to determine the email address in the article and then replace the email address.

Design Regular Expression: Determine the email expression "\ W {1, }@w {1 ,}\\.";

(3) Split () method

The split () method is actually a sharding method, which is stored in a String Array Based on matching regular expressions;

Example 3: Read all email addresses from the group email address;

Analysis: ";" is used as the separator for group mail, which must be split ";".

Basic method for constructing expressions

The constructor for constructing a RegEx object contains two overloading Methods: one is a constructor without parameters and the other is a constructor with parameters;

    • Basic Format RegEx (string pattern );
    • RegEx (string pattern, regexoptions );

Supplement: regexoptions belongs to the enumeration type, including ignorecase (case-insensitive), reghttoleft (from right to left), none (default), cultureinvariant (ignore region), and multline (multi-row mode) and singleline (single-line mode );

Example 4: Create a valid ISBN verification format;

Analysis: ISBN is a X-XXXXX-XXX-X;

Regular Expression format: \ D-\ D {5}-\ D {3}-\ D

Construct the Regular Expression Function RegEx isbnregex = new RegEx (expression, parameter is blank)

Example 5: a complex expression used to determine the format of a folder (applications that contain special symbols such as "\" and spaces)

 private void button16_click (Object sender, eventargs E) {RegEx Reg = new RegEx ("\ W * \ W {8} \-\ W * \ s \ \ W *\\~ \ S \ W * "); string STR =" Materials \ 20010202-course mathematics 1100 ~ Xu Jing "; MessageBox. Show (Reg. ismatch (STR). tostring () ;}

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.