Note: dedicated collection
In terms of speed, of course, regular expressions are the fastest and most efficient. Next we will talk about how to use regular expression grouping for collection.
First, analyze the advantages of the regular value compared to the non-regular value.
This example is a little complicated.
<Tr>
<TD class = "odd"> <a href = "/files/article/INFO/96/96231 .htm"> legend of the Emperor </a> </TD>
<TD class = "even"> <a href = "/files/article/html/96/96231/index.shtml" target = "_ blank"> 104th chapter orphan </A> </TD>
<TD class = "odd"> lonely drifting </TD>
<TD class = "even"> 449 k </TD>
<TD class = "odd" align = "center"> 07-09-16 </TD>
<TD class = "even" align = "center"> serialization </TD>
</Tr>
<Tr>
<TD class = "odd"> <a href = "/files/article/INFO/95/95119 .htm"> amazing </a> </TD>
<TD class = "even"> <a href = "/files/article/html/95/95119/index.shtml" target = "_ blank"> volume 1, Chapter 3, multiple illusion </a> </TD>
<TD class = "odd"> stainless </TD>
<TD class = "even"> 463 K </TD>
<TD class = "odd" align = "center"> 07-09-16 </TD>
<TD class = "even" align = "center"> serialization </TD>
</Tr>
If you use a previously written non-regular expression function to take the value, it is obvious that an error will occur. If I want to take the "Lonely drifting" in it, it must be
Sample getkeyvalue (htmlcode, "<TD class = \" odd \ ">", "</TD>", 1), the value of "1" in the function is a big mistake, because there is a similar
If the value is 1, "<a href ="/files/article/html/96/96231/index.shtml "target =" _ blank "> 104th chapter
Isolated heart </a> ", if the function is getkeyvalue (htmlcode," <TD class = \ "odd \"> "," </TD> ", 2) If the parameter value is 2
You can get what you want. That is to say, you have to add judgment in the function. But what if you encounter another conflict next time? The function has to be rewritten.
It is too troublesome.
If you use a regular expression to group values, you will not encounter the problem mentioned above. The function can always use one, but only the regular expression needs to be changed.
The regular expression functions are as follows:
/// <Summary>
/// Value of the regular expression
/// </Summary>
/// <Param name = "htmlcode"> Source Code </param>
/// <Param name = "regexstring"> Regular Expression </param>
/// <Param name = "groupkey"> Regular Expression grouping keyword </param>
/// <Param name = "righttoleft"> from right to left </param>
/// <Returns> </returns>
Public String [] getregvalue (string htmlcode, string regexstring, string groupkey, bool righttoleft)
{
Matchcollection m;
RegEx R;
If (righttoleft = true)
{
R = new RegEx (regexstring, regexoptions. ignorecase | regexoptions. singleline | regexoptions. righttoleft );
}
Else
{
R = new RegEx (regexstring, regexoptions. ignorecase | regexoptions. singleline );
}
M = R. Matches (htmlcode );
String [] matchvalue = new string [M. Count];
For (INT I = 0; I <M. Count; I ++)
{
Matchvalue [I] = m [I]. Groups [groupkey]. value;
}
Return matchvalue;
}
Now, to get "Lonely drifting", you only need to correct the expression.
String regexstring = "<TD class = \" odd \ "> (? <Title> [^ <]. *?) </TD> ";
String [] Title = getregvalue (htmlcode, regexstring, "title", true );
The value of title [0] is "Lonely drifting", and the value of title [1] is "stainless"
For example, if I want to obtain "<a href ="/files/article/html/96/96231/index.shtml "target =" _ blank ">
The website and title in chapter 0 </a>.
String regexstring = "<a href = \"(? <URL> .*?) \ "Target = \" _ blank \ "> (? <Title> [^ <]. *?) </A> ";
String [] url = getregvalue (htmlcode, regexstring, "url", true );
String [] Title = getregvalue (htmlcode, regexstring, "title", true );
The value of URL [0] is "/files/article/html/96/96231/index.shtml", and the value of URL [1] is "/files/article/html/95/95119/index.shtml"
The value of title [0] is "Gu mood robbery Chapter 1 Gu heart", and the value of title [1] is "Chapter 1, multi-illusion"
this article from the csdn blog, reprinted please indicate the source: http://blog.csdn.net/bingqimao/archive/2007/09/18/1789777.aspx