您现在的位置是:网站首页> 编程资料编程资料
正则表达式提取网址、标题、图片等一例(.Net Asp Javascript/Js)的实现_正则表达式_
2023-05-25
202人已围观
简介 正则表达式提取网址、标题、图片等一例(.Net Asp Javascript/Js)的实现_正则表达式_
在一些抓取、过滤等情况下, 正则表达式 regular expression 的优势是很明显的。
例如,有如下的字符串:
现在,需要提取 href 后面的网址,[]内的日期,和 链接的文字。
下面给出C#, ASP 和 Javascript 的实现方式
C#的实现
string strHTML = "[09/11]FCKEditor高亮代码插件测试 ";
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 );
}
}
ASP的实现
<%
Dim str, reg, objMatches
str = "[09/11]FCKEditor高亮代码插件测试 "
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("网址:")
Response.Write(objMatches(0).SubMatches(0))
Response.Write("
")
Response.Write("日期:")
Response.Write(objMatches(0).SubMatches(1))
Response.Write("
")
Response.Write("标题:")
Response.Write(objMatches(0).SubMatches(2))
End If
%>
Javascript的实现
例如,有如下的字符串:
复制代码 代码如下:
现在,需要提取 href 后面的网址,[]内的日期,和 链接的文字。
下面给出C#, ASP 和 Javascript 的实现方式
C#的实现
复制代码 代码如下:
string strHTML = "
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 );
}
}
ASP的实现
复制代码 代码如下:
<%
Dim str, reg, objMatches
str = "
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("网址:")
Response.Write(objMatches(0).SubMatches(0))
Response.Write("
")
Response.Write("日期:")
Response.Write(objMatches(0).SubMatches(1))
Response.Write("
")
Response.Write("标题:")
Response.Write(objMatches(0).SubMatches(2))
End If
%>
Javascript的实现
复制代码 代码如下:
您可能感兴趣的文章:
