首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

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

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

求正则表达式 提取出含有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()

热点排行