62,046
社区成员
发帖
与我相关
我的任务
分享
protected string Image()
{
string str="aaa<img alt="" src="/WebUI/Background/images/leftnav.jpg" style="width: 135px; height: 33px; " />bbbb";
string str1="<img";
string str2="/>";
int i= string.CompareOrdinal(strimg,str1);
int j= string.CompareOrdinal(strimg, str2);
}
//是要把宽高变成你想要的?
int width=100,height=100;
string str="aaa<img alt=\"\" src=\"images/p001.jpg\" style=\"width: 135px; height: 33px; \" />bbbb";
string result = Regex.Replace(str, @"(?<=<img[^>]*?)width\s*([^;]+);\s*height\s*:([^;]+);(?=[^>]*?>)", "width:" + width + "px;height:" + height + "px;");
Response.Write(result);
string str="aaa<img alt=\"\" src=\"/WebUI/Background/images/leftnav.jpg\" style\"width: 135px; height: 33px; \" />bbbb";
Regex reg = new Regex(@"(?is)<img[^>]*?src=(['""\s]?)([^'""\s]*)\1[^>]*?>");
foreach (Match m in reg.Matches(str))
{
//m.Groups[2].Value就是提取出来的地址
}
protected string Image(string str)
{
Regex re = new Regex(@"<img.*?/>", RegexOptions.None);
MatchCollection mc = re.Matches("str");
foreach (Match ma in mc)
{
//ma.Value就是你要的img标签了。。
}
if (mc.Count>0)
{
return str;
}
else
{
return "";
}
}
protected string Image(string str)
{
Regex re = new Regex(@"<img.*?/>", RegexOptions.None);
MatchCollection mc = re.Matches("str");
foreach (Match ma in mc)
{
//ma.Value就是你要的img标签了。。
}
if (mc.Count>0)
{
return str;
}
else
{
return "";
}
}
Tools.Regular.Path = Path.Replace(path, "");
public class Regular
{
public static string Path { get; set; }
public static List<string> getImgUrl(string Content)
{
//正则表达式,用于截取的是引用资源中后缀名为jpg,bmp,gif的字符串,是情况自行修改
Regex r = new Regex("src[^>]*[^/].(?:jpg|bmp|gif)");
//从源代码中获取所有截取的文本串,Content 是HTML源代码
MatchCollection mc = r.Matches(Content);
//将string串存入FilePath数组中
List<string> FilePath = new List<string>();
for (int i = 0; i < mc.Count; i++)
{
FilePath.Add(mc[i].Value.ToString());
}
//获取标准的FullPath
for (int i = 0; i < FilePath.Count; i++)
{
FilePath[i] = FilePath[i].Replace("src=\"/",Regular.Path).Replace("/","\\");
}
return FilePath;
}
}
/*物理路径*/
string Path = Request.PhysicalApplicationPath;
/*应用程序根目录*/
string path = Request.ApplicationPath.Replace("/", "\\");
/*替换获取网站实际保存路径*/
Tools.Regular.Path = Path.Replace(path, "");
Regex re = new Regex(@"(?<=<img.*?src=\"").*?(?=\"".*?/>)", RegexOptions.None);
MatchCollection mc = re.Matches("这里是你要提取的字符串");
foreach (Match ma in mc)
{
//ma.Value就是你要的SRC ,结果:/WebUI/Background/images/leftnav.jpg
}
int iStart=str.IndexOf("<img");
int iEnd=str.IndexOf("/>",iStart);
string temp=str.Substring(istart,iend-istart);
Regex re = new Regex(@"<img.*?/>", RegexOptions.None);
MatchCollection mc = re.Matches("这里是你要提取的字符串");
foreach (Match ma in mc)
{
//ma.Value就是你要的img标签了。。
}