Summary C # learning and application of regular expression classes

Source: Internet
Author: User

 

 

First, let's talk about several important regular expression classes:

 

RegEx

RegExClass indicates an unchangeable (read-only) regular expression. It also contains various static methods that allow other regular expression classes to be used without explicitly creating instances of other classes.

The followingCodeThe example is createdRegExClass instance and define a simple regular expression when initializing the object. Note that the additional backslash is used as the escape character\.The backslash in the matching character class is specified as the original literal character.

Example 1: RegEx r = new RegEx ("^. + \ W @. + \ W \. \ W + $"); // ^. + \ W @. + \ W \. \ W + $ matches the e_mail address

RegExCommon Methods of classes:

RegEx. Match
Search for the first match of the regular expression specified in the RegEx constructor in the specified input string.

RegEx. Matches
Search for all matching items of the specified Regular Expression in the specified input string.

Match

Match
Class indicates the result of a single regular expression matching operation.

The instance of the Match Class is returned by the RegEx. Match Method and indicates the first pattern match in the string. Match. nextmatch
The match object returned by the method. In addition, a matchcollection object consisting of zero, one, or more match objects consists of RegEx. Matches
Method.

Matchcollection

Matchcollection
Class indicates the sequence of successful non-overlapping matching items. The set is unchangeable (read-only) and there is no public constructor.MatchcollectionThe instance is composed
RegEx. MatchesMethod.

If the RegEx. Matches method cannot match the Regular Expression Pattern in the input string, it returns an empty matchcollection object. Then, you can use
The foreach construction in C # Or the for each construction in Visual Basic to cyclically access the set.

 

Run the following code to extract the specified content from the specified text (extract the exercise questions with a hundred surnames ):

 1  String Files = file. readalltext ( @"  F: \ bjx.txt "  , Encoding. Default );  2   3              4   5               String Reg = @"  [\ W {1 }〕  "  ;  6 Matchcollection MAT = RegEx. Matches (files, Reg ); //  Regular Expression Extraction  7   8   9   10 List < String > AA = New List < String > (); //  Generic Application  11             12                //  Foreach Traversal  13              Foreach (Match m In Mat) // Match Class indicates the matching operation result of a single Regular Expression  14   {  15   16                                  17 AA. Add (M. value ); //  Save the search result to a generic variable.  18   }  19   20         21               For ( Int I = 0 ; I <AA. Count; I ++ )  22   {  23 Console. writeline (AA [I]); //  Output to screen  24   }  25              26 Console. readkey ();

Add a bit of knowledge to match only Chinese characters:

[\ U2e80-\ u9fff] +
Match all languages in East Asia
[\ U4e00-\ u9fff] +
Match between simplified and Traditional Chinese
[\ U4e00-\ u9fa5] +
Matching simplified

For example, [\ u2e80-\ u9fff |,] contains Chinese characters with punctuation marks.

Tested.

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.