C#后台正则获取内容替换
代码是下面这样的
<p>
<embed src="/editor/kindeditor/attached/media/20130626/20130626065054_3546.avi" type="video/x-ms-asf-plugin" width="550" height="400" autostart="false" loop="true" />
</p>
<p>
addw
</p>
<p>
<embed src="/editor/kindeditor/attached/media/20130626/20130626084219_0577.avi" type="video/x-ms-asf-plugin" width="550" height="400" autostart="true" loop="true" />
</p>
我想取出红色的内容外层标签用flash播放器替代。蓝色内容不固定。
逻辑不是很会。 C# 正则
[解决办法]
是想根据src找到该标签吗?
string tempStr = File.ReadAllText(@"C:\Users\myx\Desktop\Test.txt", Encoding.GetEncoding("GB2312"));//读取txt
string src="/editor/kindeditor/attached/media/20130626/20130626065054_3546.avi";
string pattern = @"(?i)<embed[^>]*?src=(['""]?)"+src+@"\1[^>]*?>";
tempStr = Regex.Replace(tempStr,pattern,"替换内容");
string regex = @"(?i)<embed[^>]*src=['""]?([^'""]+)\1[^/>]*?>",
flashMask = "<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="..."><param name="movie" value="$1" /><param name="quality" value="high"/></object>";
tempStr = Regex.Replace(html, regex, flashMask);
string regex = @"(?i)<embed\b[^>]*?src=(['""]?)([^'""]+)\1[^>]*?autostart=\1(?:true
[解决办法]
false)\1[^>]*?>",
flashMask = "<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="..."><param name="movie" value="$2" /><param name="quality" value="high"/></object>";
result= Regex.Replace(html, regex, flashMask);
string tempStr = File.ReadAllText(@"C:\Users\myx\Desktop\Test.txt", Encoding.GetEncoding("GB2312"));//读取txt
string pattern = @"(?i)<embed[^>]*?src=(['""]?)([^'""]*?)\1[^>]*?>";
tempStr = Regex.Replace(tempStr, pattern, a => {
string src = a.Groups[2].Value;
string result = string.Empty+src;//根据src拼接结果
return result;
});
string html = @"<p>
<embed src=""/editor/kindeditor/attached/media/20130626/20130626065054_3546.avi"" type=""video/x-ms-asf-plugin"" width=""550"" height=""400"" autostart=""false"" loop=""true"" />
</p>
<p>
addw
</p>
<p>
<embed src=""/editor/kindeditor/attached/media/20130626/20130626084219_0577.avi"" type=""video/x-ms-asf-plugin"" width=""550"" height=""400"" autostart=""true"" loop=""true"" />
</p>";
string regex = @"(?is)<embed\s*[^>]*?src=(['""]?)([^'""]+)\1[^>]*?(width=\1\d+\1)\s*(height=\1\d+\1)\s*autostart=\1(?:true
[解决办法]
false)\1[^>]*?>",
flashMask = "<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="..."><param name="movie" $3 $4 value="$2" /><param name="quality" value="high"/></object>";
html = Regex.Replace(html, regex, flashMask);