====================================
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.