Regular Expression in C #

Source: Internet
Author: User

Regular Expressions are what I like. In terms of string processing, regular expressions have unparalleled advantages in addition to performance issues. C # introduces the regular expression class library, which brings us great convenience.
To use a regular expression in C #, you must reference the following namespace:

Using system. text;
Using system. Text. regularexpressions;

Use regular expressions in C #. The most important class is RegEx. The most common constructor parameters of a RegEx object are regular expression strings, and regexoptions is frequently used. The regular expression string and the regular expression syntax are not too long here. If you are interested, you can write an email to discuss it with me. The following describes some of the main parameters of regexoptions:
Regexoptions. Compiled allows C # To compile the regular expression into an assembly, which can be started faster when the regular expression is executed. When using this option, the regular expression must be a static string instead of a dynamic string (as you can imagine, dynamic strings do not have any effect ).
Regexoptions. ignorecase ignores the case when the regular expression matches.
Regexoptions. multiline pattern regular match.
Regexoptions. None does not specify any options.
Regexoptions. righttoleft starts matching from right to left.
Regexoptions. singleline single-line pattern regular match.

The following describes the use of regular expressions:
RegEx token = new RegEx (@"((? <Protocol> [A-Za-Z] *?) ://)? (? <Domain> [^/] *) (? <Path>. *) ", regexoptions. Compiled );
Match matchlist = token. Match ("http://www.sina.com.cn/index.html ");

If (matchlist. success)
{< br> console. writeline ("Protocol: {0}, domain: {1}, path: {2}",
matchlist. groups ["protocol"],
matchlist. groups ["Domain"],
matchlist. groups ["path"]);
}< br>
the preceding example matches a single row. The following example shows how to match multiple results:

RegEx token = new RegEx (@ "\ s *((? <Protocol> \ W *?) ://)? (? <Domain> [^ \/\ s] + )(? <Path> [^ \ s] *) ", regexoptions. Compiled );
Matchcollection matches = token. Matches ("http://www.sina.com.cn/index.html http://www.microsoft.com ftp://ftp.cav.com/info.zip ");

If (matches. Count! = 0)
{
Foreach (match matchlist in matches)
{
Console. writeline ("Protocol: {0}, domain: {1}, path: {2 }",
Matchlist. Groups ["protocol"],
Matchlist. Groups ["Domain"],
Matchlist. Groups ["path"]);

foreach (capture C in matchlist. captures)
{< br> console. writeline ("Capture: [" + C + "]");
}< BR >}

The following describes how to use replace to demonstrate matchevaluator.
Public static string replaceeva (match)
{
Return match. Groups ["protocol"]. Value + ": //" + "www.google.com/" + match. Groups ["path"]. value;
}

RegEx token = new RegEx (@ "\ s *((? <Protocol> \ W *?) ://)? (? <Domain> [^ \/\ s] + )(? <Path> [^ \ s] *) ", regexoptions. Compiled );
String strnew = token. Replace ("www.sohu.com/index.htm", new matchevaluator (replaceeva ));
Console. writeline ("New Line: {0}", strnew );

Matchevaluator is a function proxy used to process each match obtained during the replacement process. The returned string is the replaced string.

The following is a simple example of using regular expressions for splite.CodeAs follows:

RegEx r = new RegEx ("(-)");
String [] S = R. Split ("one-two-banana ");

OK, that's all. Hope to help you.

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.