当前位置: 代码迷 >> .NET新技术 >> 求正则表达式 提掏出含有src属性中timeType文字的图片 并保存到数据库 同时将 生成的编号更新到alt 属性中
  详细解决方案

求正则表达式 提掏出含有src属性中timeType文字的图片 并保存到数据库 同时将 生成的编号更新到alt 属性中

热度:244   发布时间:2016-04-25 01:55:08.0
求正则表达式 提取出含有src属性中timeType文字的图片 并保存到数据库 同时将 生成的编号更新到alt 属性中
求正则表达式 ,提取出含有src属性中timeType文字的图片 并保存到数据库 同时将 生成的编号更新到alt 属性中
如:
<img title="" height="19" alt="0" width="36" score="0" src="../../ImportWord/images/timuType1.gif" />
<img title="" alt="0" width="212" height="19" score="0" src="../../ImportWord/images/timuType2.gif" />
<img src="abc.gif"/>
<img title="" alt="0" width="212" height="19" score="0" src="../../ImportWord/images/timuType2.gif" />
<img src="abc.gif"/>
<img title="" width="212" height="19" alt="0" score="0" src="../../ImportWord/images/timuType3.gif" />
图片的属性 摆放顺序不一样

结果输出:
<img title="" height="19" alt="1" width="36" score="0" src="../../ImportWord/images/timuType1.gif" />
<img title="" alt="2" width="212" height="19" score="0" src="../../ImportWord/images/timuType2.gif" />
<img title="" alt="3" width="212" height="19" score="0" src="../../ImportWord/images/timuType2.gif" />
<img title="" width="212" height="19" alt="4" score="0" src="../../ImportWord/images/timuType3.gif" />
 这个正则表达式可以提取出图片 ,可是不知道怎么更新编号到alt属性中 
 Regex reg = new Regex(@"(?is)(<img(\s+title=""(?<title>[^""]*)""|\s+height=""(?<height>[^""]*)""|\s+alt=""(?<alt>[^""]*)""|\s+width=""(?<width>[^""]

*)""|\s+score=""(?<score>[^""]*)""|\s+src=""[^""]*timuType(?<type>\d+)\.gif"")+[ /]*>)");




------解决方案--------------------
提取和替换要分两步走..
提取后存入数据库后
你再替换:
string str = "<img .. alt=.. />";
str = Regex.Replace(str, @"(?i)(?<=alt="")[^""]*", "your编号");
------解决方案--------------------
你取匹配结果是用的循环吧,怎么会得不到自增编号?

------解决方案--------------------
C# code
        Regex reg = new Regex("...");        MatchCollection mc = reg.Matches("..........");        for (int i = 0; i < mc.Count; i++)        {            //取值            string value = mc[i].Value;            string alt = mc[i].Groups["alt"].Value;            //....            //替换为编号            value = Regex.Replace(value, @"(?i)(?<=alt="")[^""]*", i.ToString());        }
------解决方案--------------------
问一下,6楼的你能理解吗??
把i.ToString() 改为 (i+1).ToString()