Simple use of regular expressions in C #

Source: Internet
Author: User

The class for regular expressions in C # is contained in the System.Text.RegularExpressions namespace, which can be added by code:

1 using System.Text.RegularExpressions;
View Code

In string matching, the protagonists are "matched strings" and "matching patterns".

1 //define "matched string"2 stringMessage ="mother father Sister brother";3 //define "match pattern"4 stringPattern =@"\b (\w+) ther\b";5 //match by static method, or by creating a regular object6MatchCollection matches =regex.matches (message, pattern);7 //the resulting traversal8 foreach(Match matchinchmatches) {9Console.WriteLine ("Groups[0]. Value = {0}---groups[1]. Value = {1}", match. groups[0]. Value, matchgroups[1]. Value); Ten }  One  A //Output - //Groups[0]. Value = Mother---groups[1]. Value = Mo - //Groups[0]. Value = Father---groups[1]. Value = FA the //Groups[0]. Value = Brother---groups[1]. Value = Bro - //End Output

This is one of the examples where there are three ways to match: IsMatch (), Match (), Matches ().

If you just want to know if the "matched string" contains a certain "match pattern", you can use IsMatch ();

If you want a match to the result, you can use match ();

If you want to get multiple matching results, you can use matches ();

A single match to the result can use groups[] to get a grouping that matches the matching pattern to take advantage of the information that is being matched.

Specific usage rules: Http://www.dotnetperls.com/regex-match

Simple use of regular expressions in C #

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.