C # regular expression Match class and Group class usage

Source: Internet
Author: User

====================================

MatchCollection mc = Regex. Matches ("A12493", "[A-Za-z]");
Foreach (Match m in mc)
{
String a = Convert. ToString (m. Groups ["0"]. Value );
}

 

 

========================================================== ================================

The following example demonstrates how to convert the above UBB code into HTML code:


///
/// The following code replaces the UBB hyperlink code in the text with the HTML hyperlink code
///
Public void UBBDemo ()
{
String text = "[url = http://zhoufoxcn.blog.51cto.com] [/url] [url = http://blog.csdn.net/zhoufoxcn] [/url]";
Console. WriteLine ("original UBB Code:" + text );
Regex regex = new Regex (@ "(\ [url = ([\ S \ t] *?) \]) ([^ [] *) (\ [\/Url \]) ", RegexOptions. IgnoreCase );
MatchCollection matchCollection = regex. Matches (text );
Foreach (Match match in matchCollection)
{
String linkText = string. Empty;
// If the link text is included, if the second UBB Code contains a link name, use the Link name directly.
If (! String. IsNullOrEmpty (match. Groups [3]. Value ))
{
LinkText = match. Groups [3]. Value;
}
Else // otherwise, use the link as the Link name
{
LinkText = match. Groups [2]. Value;
}
Text = text. Replace (match. Groups [0]. Value, "" + linkText + "");
}
Console. WriteLine ("replaced Code:" + text );
  
}

The program execution result is as follows:

Original UBB code: [url = http://zhoufoxcn.blog.51cto.com] [/url] [url = http://blog.csdn.net/zhoufoxcn] [/url]

Code after replacement:Http://zhoufoxcn.blog.51cto.comZhou Gong's column

The above example is a little complicated. It may be difficult for beginners to understand regular expressions, but it doesn't matter. I will talk about regular expressions later. In actual conditions, it is possible to use match. groups [0]. the Value method is not convenient, so you want to write string name = DataTable when accessing the able. rows [I] [j] is the same method. Once it is adjusted again, this index method is very prone to errors. In fact, we can also use names instead of indexes to access Group groups, this will also be discussed in the future.

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.