In some situations such as crawling and filtering, the advantage of the regular expression is obvious.
For example, there are the following strings:
CopyCode The Code is as follows: <li> <a href = "http://www.abcxyz.com/something/article/143.htm" Title = "FCKeditor highlight code plug-in test"> <SPAN class = "Article-Date"> [09/11] </span> FCKeditor highlight code plug-in test </a> </LI>
now, you need to extract the URL following href, the date in [], and the link text.
The following describes how to implement C #, ASP, and Javascript.
Implementation of C # copy Code the code is as follows: string strhtml = "
[09/11] FCKeditor highlight code plug-in test ";
string pattern = "http: // ([^ \ s] + )\". +? Span. +? \ [(. + ?) \]. +?> (. + ?) <";
RegEx Reg = new RegEx (pattern, regexoptions. ignorecase);
matchcollection MC = reg. matches (strhtml);
If (MC. count> 0)
{< br> foreach (Match m in MC)
{< br> console. writeline (M. groups [1]. value);
console. writeline (M. groups [2]. value);
console. writeline (M. groups [3]. value);
}< BR >}
Asp implementation copy Code the code is as follows: <%
dim STR, Reg, objmatches
STR = "
[09/11] FCKeditor highlight code plug-in test "
set Reg = new Regexp
Reg. ignorecase = true
Reg. global = true
Reg. pattern = "Http: // ([^ \ s] +)" ". +? Span. +? \ [(. + ?) \]. +?> (. + ?) <"
set objmatches = reg. execute (STR)
If objmatches. count> 0 then
response. write ("url:")
response. write (objmatches (0 ). submatches (0)
response. write ("
")
response. write ("Date:")
response. write (objmatches (0 ). submatches (1)
response. write ("
")
response. write ("title:")
response. write (objmatches (0 ). submatches (2)
end if
%>
JavaScript implementation copy Code the code is as follows: