C#.net中的正则表达式 [英] Reqular expressions in c# .net

查看:130
本文介绍了C#.net中的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

你能告诉我正则表达式来分割图像src

例如字符串如下

Hi All,

Can u tell me regular expression to split image src

For example string is like below

string testing="<img src=\"cid:tmpImage.gif\">,cdefetfdgfdg ,ewrewrewr,<img src=\"cid:tmpImage1.gif\">,<img src=\"cid:tmpImage2.gif\">,<img src=\"cid:tmpImage3.gif\">,<img src=\"cid:tmpImage4.gif\">";
    string[] splitText = Regex.Split(testing, @"(<img([^>]+)>)");




分割后,我需要以下形式


src =''images/img1.jpg''
src =''images/img2.jpg''
src =''images/img3.jpg''
src =''images/img4.jpg''


感谢




after split I need in the following form


src=''images/img1.jpg''
src=''images/img2.jpg''
src=''images/img3.jpg''
src=''images/img4.jpg''


Thanks

推荐答案

使用Regex可以找到许多内容,但是在这种情况下,某些HTML解析器可能会做得更好.如果此HTML是格式正确的XML,则非常容易,因为XML解析器(至少其中三个)在.NET中很容易获得.如果HTML格式不正确,那就更糟了,但是您可以使用一些可用的HTML解析器来处理.试试这个:
http://www.majestic12.co.uk/projects/html_parser.php [ ^ ].

—SA
Many things can be found with Regex, but in this case some HTML parser could do better. It''s very easy if this HTML is well-formed XML, as XML parsers (at least three of them) are readily available in .NET. If the HTML is not well-formed, it''s much worse, but you can use some available HTML parser which can deal with that. Try this one:
http://www.majestic12.co.uk/projects/html_parser.php[^].

—SA



看看下面的代码是否有帮助

Hi,
See if following piece of code helps

string testing = "<img src="\"cid:tmpImage.gif\"">,cdefetfdgfdg ,ewrewrewr,<img src="\"cid:tmpImage1.gif\"">,<img src="\"cid:tmpImage2.gif\"">,<img src="\"cid:tmpImage3.gif\"">,<img src="\"cid:tmpImage4.gif\"">";
Regex regex = new Regex(@"(src([^>]+))");
MatchCollection cl = regex.Matches(testing);

foreach (Match match in cl)
{
    Console.WriteLine(match.Value);
}



它给我的输出为



Its giving me output as

src="cid:tmpImage.gif"
src="cid:tmpImage1.gif"
src="cid:tmpImage2.gif"
src="cid:tmpImage3.gif"
src="cid:tmpImage4.gif"



希望能达到目的.如果有帮助,请将答案标记为解决方案和/或赞成.

谢谢
Milind



Hope that serves the purpose. If it helps, mark the answer as solution and/or upvote.

Thanks
Milind


这篇关于C#.net中的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆