In some cases of grasping and filtering, the advantage of regular expression regular expression is obvious.
For example, a string that resembles the following:
Copy Code code 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 URLs behind the href, [] within the date, and the linked text.
Here's how C #, ASP, and Javascript are implemented
implementation of C #
Copy Code code as follows:
String strhtml = "<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 highlighting code plug-in test </a></ Li> ";
String pattern = "http://([^\\s]+) \". +?span.+?\\[(. +?) \\].+?> (. +?) < ";
Regex reg = new Regex (pattern, regexoptions.ignorecase);
MatchCollection mc = Reg. Matches (strhtml);
if (MC. Count > 0)
{
foreach (Match m in MC)
{
Console.WriteLine (M.groups[1]. Value);
Console.WriteLine (M.groups[2]. Value);
Console.WriteLine (M.groups[3]. Value);
}
}
implementation of ASP
Copy Code code as follows:
<%
Dim str, Reg, objmatches
str = "<li><a href=" "http://localhost/Z-Blog18/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>"
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 ("<br>")
Response.Write ("Date:")
Response.Write (objmatches (0). Submatches (1))
Response.Write ("<br>")
Response.Write ("title:")
Response.Write (objmatches (0). Submatches (2))
End If
%>
the implementation of JavaScript
Copy Code code as follows:
<script type= "Text/javascript" >
var str = ' <li><a href= ' http://localhost/Z-Blog18/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> ';
var pattern =/http:\/\/([^\s]+) ". +?span.+?\[(. +?) \].+?> (. +?) </gi;
var mts = pattern.exec (str);
if (MTS!= NULL)
{
Alert (mts[1]);
Alert (mts[2]);
Alert (mts[3]);
Alert (mts[4]);
}
</script>