Use of the Regex class for C # regular expressions

Source: Internet
Author: User

C # provides a very powerful function for the use of regular expressions, which is the Regex class. This package is included under the System.Text.RegularExpressions namespace, and the DLL where the namespace is located basically does not need to be added separately in all project templates and can be used directly.

1. Define an instance of a Regex class


Regex regex = new Regex (@ "\d");
The initialization parameter here is a regular expression, and "\d" represents the configuration number.

2. Whether the match is judged

Determines whether a string matches a regular expression, and in a Regex object, you can use the Regex.IsMatch (string) method.

Regex. IsMatch ("abc"); The return value is False, the string does not contain a number
Regex. IsMatch ("Abc3abc"); The return value is true because the string contains a number

3. Get the number of matches

Use the Regex.Matches (string) method to get a Matches collection, and then use the Count property of this collection.

Regex. Matches ("Abc123abc"). Count;
The return value is 3 because the number is matched three times.

4. Get the matching content

Use the Regex.match (string) method to match.

Regex. Match ("Abc123abc"). Value;
A return value of 1 indicates the first match to the value.

5. Capture

You can use parentheses to capture part of a value in a regular expression, and to get the captured value, you can Regex.match (string). Groups[int]. Value to obtain.

Regex regex = new Regex (@ "\w (\d*) \w"); Matches a two-letter string of numbers
Regex. Match ("Abc123abc"). Groups[0]. Value; The return value is "123".

usingSystem; usingSystem.Text.RegularExpressions; namespaceMagci.Test.Strings { Public classTestregular { Public Static voidWritematches (stringstr, matchcollection matches) {Console.WriteLine ("\nstring is:"+str); Console.WriteLine ("No. of matches:"+matches.                          Count); foreach(Match NextMatchinchmatches) {                                  //Take out a matching string and up to 10 peripheral characters                intIndex =Nextmatch.index; stringresult =nextmatch.tostring (); intCharsbefore = (Index <5) ? Index:5; intFromend = str. Length-index-result.                                  Length; intCharsafter = (Fromend <5) ? Fromend:5; intCharstodisplay = Charsbefore + result. Length +Charsafter; Console.WriteLine ("Index: {0},\tstring: {1},\t{2}", Index, result, str. Substring (Index-Charsbefore, Charstodisplay)); }                  }                            Public Static voidMain () {stringSTR =@"My name is MAGCI, for short MGC. I like C sharp!"; //find "GC"            stringPattern ="GC"; MatchCollection Matches= Regex.Matches (Str, Pattern, Regexoptions.ignorecase |regexoptions.explicitcapture);                                        Writematches (STR, Matches); //find words that begin with "M" and End With "C"Pattern =@"\bm\s*c\b"; Matches= Regex.Matches (Str, Pattern, Regexoptions.ignorecase |regexoptions.explicitcapture);                  Writematches (STR, Matches); }          }  } 

Use of the Regex class for C # regular expressions

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.