代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
正则取出img中src
作者:
scottckt
/ 发布于
2011/6/22
/
729
<blockquote> <div> 有下例Html代码 <div style="width: 900px" class="cnblogs_code"> <div><!--<br/ /> <br/ /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ /> http://www.CodeHighlighter.com/<br/ /> <br/ /> --><span style="color: #0000ff"><</span><span style="color: #800000">div</span><span style="color: #0000ff">></span><span style="color: #000000"> </span><span style="color: #0000ff"><</span><span style="color: #800000">img </span><span style="color: #ff0000">alt</span><span style="color: #0000ff">='' </span><span style="color: #ff0000">src</span><span style="color: #0000ff">="/UpLoad/image/111.jpg"</span><span style="color: #ff0000"> </span><span style="color: #0000ff">/><</span><span style="color: #800000">img </span><span style="color: #ff0000">alt</span><span style="color: #0000ff">='' </span><span style="color: #ff0000">src</span><span style="color: #0000ff">='/UpLoad/image/222.jpg' </span><span style="color: #0000ff">/><</span><span style="color: #800000">img </span><span style="color: #ff0000">src</span><span style="color: #0000ff">='./UpLoad/image/333.jpg' </span><span style="color: #ff0000">alt</span><span style="color: #0000ff">='' </span><span style="color: #0000ff">/></</span><span style="color: #800000">div</span><span style="color: #0000ff">></span><span style="color: #000000"> </span><span style="color: #0000ff"><</span><span style="color: #800000">div</span><span style="color: #0000ff">></span><span style="color: #000000"> </span><span style="color: #0000ff"><</span><span style="color: #800000">img </span><span style="color: #ff0000">alt</span><span style="color: #0000ff">='' </span><span style="color: #ff0000">src</span><span style="color: #0000ff">='/UpLoad/image/444.jpg' </span><span style="color: #0000ff">/></span><span style="color: #000000"> </span><span style="color: #0000ff"><</span><span style="color: #800000">img </span><span style="color: #ff0000">src</span><span style="color: #0000ff">='../sa/UpLoad/image/333.jpg' </span><span style="color: #ff0000">alt</span><span style="color: #0000ff">='' </span><span style="color: #0000ff">/></span><span style="color: #000000"> </span><span style="color: #0000ff"></</span><span style="color: #800000">div</span><span style="color: #0000ff">></span></div> </div> 现在要取出img中的src。正则的写法如下: <div style="width: 900px" class="cnblogs_code"> <div><!--<br/ /> <br/ /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ /> http://www.CodeHighlighter.com/<br/ /> <br/ /> --><span style="color: #000000"><</span><span style="color: #000000">img[</span><span style="color: #000000">^></span><span style="color: #000000">]</span><span style="color: #000000">*</span><span style="color: #000000">src\s</span><span style="color: #000000">*=</span><span style="color: #000000">\s</span><span style="color: #000000">*</span><span style="color: #000000">[</span><span style="color: #800000">'</span><span style="color: #800000">"]([\w/\-\.]*)[</span><span style="color: #800000">'"</span><span style="color: #800000">][^>]*</span></div> </div> 解释: <span style="color: #000000"><</span><span style="color: #000000">img[</span><span style="color: #000000">^></span><span style="color: #000000">]</span><span style="color: #000000">*</span> :查找以<img开头,以>终止的内容。 <span style="color: #000000">src\s</span><span style="color: #000000">*=</span><span style="color: #000000">\s</span><span style="color: #000000">*</span><span>[</span><span>'</span><span>"]([\w/\-\.]*)[</span><span>'"</span><span>][^>]*</span>:查找src开头,以>终止的内容,并取src中内容。 \s* :0或多个空格 <span>[</span><span>'</span><span>"]</span> <span>:</span>取一个'或“。 <span>([\w/\-\.]*)</span><span> :</span>取单词字符,包含_/-.等字符。(),表示用<span style="color: #000000">Groups</span>方式取到值。 用法: <div style="width: 900px" class="cnblogs_code"> <div><!--<br/ /> <br/ /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ /> http://www.CodeHighlighter.com/<br/ /> <br/ /> --><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> FindImageInXmlMethod() { </span><span style="color: #0000ff">string</span><span style="color: #000000"> ImgXml </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000"> <div><img alt='' src=\</span><span style="color: #800000">"</span><span style="color: #000000">/</span><span style="color: #000000">UpLoad</span><span style="color: #000000">/</span><span style="color: #000000">image</span><span style="color: #000000">/</span><span style="color: #800080">111</span><span style="color: #000000">.jpg\</span><span style="color: #800000">"</span><span style="color: #800000"> /><img alt='' src='/UpLoad/image/222.jpg' /><img src='./UpLoad/image/333.jpg' alt='' /></div></span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #800000">@"</span><span style="color: #800000"><div><img alt='' src='/UpLoad/image/444.jpg' /> <img src='../sa/UpLoad/image/333.jpg' alt='' /> </div></span><span style="color: #800000">"</span><span style="color: #000000">; </span><span style="color: #0000ff">string</span><span style="color: #000000"> RegEx </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000"><img[^>]*src\\s*=\\s*['\</span><span style="color: #800000">"</span><span style="color: #000000">]([\\w</span><span style="color: #000000">/</span><span style="color: #000000">\\</span><span style="color: #000000">-</span><span style="color: #000000">\\.]</span><span style="color: #000000">*</span><span style="color: #000000">)[</span><span style="color: #800000">'</span><span style="color: #800000">\"][^>]*";</span><span style="color: #800000"> </span><span style="color: #000000">Regex imgRegex </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> Regex(RegEx,RegexOptions.IgnoreCase); MatchCollection matchCollection </span><span style="color: #000000">=</span><span style="color: #000000"> imgRegex.Matches(ImgXml); </span><span style="color: #0000ff">foreach</span><span style="color: #000000"> (Match matchItem </span><span style="color: #0000ff">in</span><span style="color: #000000"> matchCollection) { </span><span style="color: #0000ff">string</span><span style="color: #000000"> val </span><span style="color: #000000">=</span><span style="color: #000000"> matchItem.Value; val </span><span style="color: #000000">=</span><span style="color: #000000"> matchItem.Groups[</span><span style="color: #800080">1</span><span style="color: #000000">].Value; Response.Write(val </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000"><br/></span><span style="color: #800000">"</span><span style="color: #000000">); } }</span></div> </div> 结果: <div style="width: 900px" class="cnblogs_code"> <div><!--<br/ /> <br/ /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ /> http://www.CodeHighlighter.com/<br/ /> <br/ /> --><span style="color: #000000">/</span><span style="color: #000000">UpLoad</span><span style="color: #000000">/</span><span style="color: #000000">image</span><span style="color: #000000">/</span><span style="color: #800080">111</span><span style="color: #000000">.jpg </span><span style="color: #000000">/</span><span style="color: #000000">UpLoad</span><span style="color: #000000">/</span><span style="color: #000000">image</span><span style="color: #000000">/</span><span style="color: #800080">222</span><span style="color: #000000">.jpg .</span><span style="color: #000000">/</span><span style="color: #000000">UpLoad</span><span style="color: #000000">/</span><span style="color: #000000">image</span><span style="color: #000000">/</span><span style="color: #800080">333</span><span style="color: #000000">.jpg </span><span style="color: #000000">/</span><span style="color: #000000">UpLoad</span><span style="color: #000000">/</span><span style="color: #000000">image</span><span style="color: #000000">/</span><span style="color: #800080">444</span><span style="color: #000000">.jpg ..</span><span style="color: #000000">/</span><span style="color: #000000">sa</span><span style="color: #000000">/</span><span style="color: #000000">UpLoad</span><span style="color: #000000">/</span><span style="color: #000000">image</span><span style="color: #000000">/</span><span style="color: #800080">333</span><span style="color: #000000">.jpg </span></div> </div> </div> </blockquote>
试试其它关键字
取出img中src
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
scottckt
贡献的其它代码
(
10
)
.
CommandArgument绑定多个值
.
js对Url进行utf8编码与解码
.
对Url进行utf8编码与解码
.
正则取出img中src
.
js操作FCKeditor方法
.
sql中escape用法
.
System.DBNull的用法
.
DataGridView控件隔行换色
.
得到应用程序所在路径
.
DataGirdView 自定义进度条列
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3