100分请教个简单的正则表达式写法

snowbirdfly 2009-11-04 03:59:06

<div class="pic"><a href="http://www.mbaobao.com/pshow-10223010.html" target="_blank"><img src="http://images.mbaobao.com/fansface/10223010/s.gif" width="160" height="160" alt="[FansFace]运动休闲两用旅行包 黑色" /></a></div>
<div class="text">
<div class="title"><a href="http://www.mbaobao.com/pshow-10223010.html" target="_blank" title="[FansFace]运动休闲两用旅行包 黑色">[FansFace]运动休闲两用旅行包 黑色</a></div>
市场价:<s>398.00</s><br />
麦包价:<span class="price">159.00</span><br />
span class="buy">
<a class="hand" onclick="add_goods_to_cart(3897);" rel="nofollow"><img src="http://www.mbaobao.com/templates/default/images/btn_cart.gif" width="87" height="22" alt="加入购物车" /></a> 
<a class="hand" onclick="add_goods_to_favorites(3897);" rel="nofollow"><img src="http://www.mbaobao.com/templates/default/images/btn_favor.gif" width="50" height="22" alt="收藏" /></a>
</span>
</div>

从这个字符串信息中获取到如下几个信息:
http://www.mbaobao.com/pshow-10223010.html(href链接网址)
http://images.mbaobao.com/fansface/10223010/s.gif(图片地址)
398.00(市场价)
159.00(麦包价)
谢谢~~~
...全文
218 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
-过客- 2009-11-04
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 snowbirdfly 的回复:]
非常感谢lxcnn,是我的原因,获取的页面编码方式出了点问题,问题解决了.
我也会好好拜读你的文章,应该会收获很多,呵呵~~~
[/Quote]

呵呵,客气,有问题欢迎交流
snowbirdfly 2009-11-04
  • 打赏
  • 举报
回复
非常感谢lxcnn,是我的原因,获取的页面编码方式出了点问题,问题解决了.
我也会好好拜读你的文章,应该会收获很多,呵呵~~~
xiaoxin4321 2009-11-04
  • 打赏
  • 举报
回复
那个,你确定你测试的字符串是否取到了你上面所提到字符串?
-过客- 2009-11-04
  • 打赏
  • 举报
回复
如果取得的源代码与楼主顶楼所给的一致,应该是可以取得的

            string test = @"<div class=""pic""><a href=""http://www.mbaobao.com/pshow-10223010.html"" target=""_blank""><img src=""http://images.mbaobao.com/fansface/10223010/s.gif"" width=""160"" height=""160"" alt=""[FansFace]运动休闲两用旅行包 黑色"" /></a></div>
<div class=""text"">
<div class=""title""><a href=""http://www.mbaobao.com/pshow-10223010.html"" target=""_blank"" title=""[FansFace]运动休闲两用旅行包 黑色"">[FansFace]运动休闲两用旅行包 黑色</a></div>
市场价:<s>398.00</s><br />
麦包价:<span class=""price"">159.00</span><br />
span class=""buy"">
<a class=""hand"" onclick=""add_goods_to_cart(3897);"" rel=""nofollow""><img src=""http://www.mbaobao.com/templates/default/images/btn_cart.gif"" width=""87"" height=""22"" alt=""加入购物车"" /></a> 
<a class=""hand"" onclick=""add_goods_to_favorites(3897);"" rel=""nofollow""><img src=""http://www.mbaobao.com/templates/default/images/btn_favor.gif"" width=""50"" height=""22"" alt=""收藏"" /></a>
</span>
</div>";
Regex reg = new Regex(@"(?is)<div class=""pic""><a href=""(?<url>[^""]*)""[^>]*><img src=""(?<img>[^""]*)""[^>]*>(?:(?!市场价:).)*市场价:<s>(?<p>[\d.]+)(?:(?!麦包价:).)*麦包价:<span[^>]*>(?<mp>[\d.]+)</span>");
MatchCollection mc = reg.Matches(test);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups["url"].Value + "\n";
richTextBox2.Text += m.Groups["img"].Value + "\n";
richTextBox2.Text += m.Groups["p"].Value + "\n";
richTextBox2.Text += m.Groups["mp"].Value + "\n";
}
/*-----------输出-------------
http://www.mbaobao.com/pshow-10223010.html
http://images.mbaobao.com/fansface/10223010/s.gif
398.00
159.00
*/
xiaoxin4321 2009-11-04
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 snowbirdfly 的回复:]
感谢11楼回复,我测试了,没有得到相关数据,但是还是非常感谢,呵呵~~~

[/Quote]
我是测试了再贴的呢~
xiaoxin4321 2009-11-04
  • 打赏
  • 举报
回复
Regex re2 = new Regex(@"([\d.]+)</s");
string price = re2.Matches(temp)[0].ToString();
price = re2.Replace(price, "$1");
这个是市场价,第二个卖包价也是类似的方法换成1就好了~
-过客- 2009-11-04
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 snowbirdfly 的回复:]
6楼的方法,我测试下,貌似没有得到数据,我正在拜读学习您的博客里面关于正则表达式的原文,写得非常好,收益很多,以后这方面不懂的望能赐教!
[/Quote]

呵呵,目前的环境只能访问指定的网站,家里这几天断网,估计只能等家里的网络好了再分析了
snowbirdfly 2009-11-04
  • 打赏
  • 举报
回复
感谢11楼回复,我测试了,没有得到相关数据,但是还是非常感谢,呵呵~~~
xiaoxin4321 2009-11-04
  • 打赏
  • 举报
回复
Regex regHttpUrl = new Regex(@"http:[\w\d-.\/\/:]*");
string Httpurl = regHttpUrl.matches(yourstring)[0].tostring();
string Imageurl = regHttpUrl.matches(yourstring)[1].tostring();
这个就能取到两个网址了吧,你试试
liaoyukun111 2009-11-04
  • 打赏
  • 举报
回复
UP
snowbirdfly 2009-11-04
  • 打赏
  • 举报
回复
6楼的方法,我测试下,貌似没有得到数据,我正在拜读学习您的博客里面关于正则表达式的原文,写得非常好,收益很多,以后这方面不懂的望能赐教!
snowbirdfly 2009-11-04
  • 打赏
  • 举报
回复
恩,很感谢大家的回复,对了,数据格式是有规律的,具体的测试页面是:
http://www.mbaobao.com/
或者其中的某个页面,http://www.mbaobao.com/c-41/
然后通过下面的代码:
string url = "http://www.mbaobao.com/c-41/";
WebClient client = new WebClient();
byte[] page = client.DownloadData(url);
string content = System.Text.Encoding.GetEncoding("gb2312").GetString(page);

就是想获取content 里面的图片的URL 图片的title 图片的价格等信息
Jave.Lin 2009-11-04
  • 打赏
  • 举报
回复
学习。
-过客- 2009-11-04
  • 打赏
  • 举报
回复
格式固定吗?看这样满不满足你的需求吧


            Regex reg = new Regex(@"(?is)<div class=""pic""><a href=""(?<url>[^""]*)""[^>]*><img src=""(?<img>[^""]*)""[^>]*>(?:(?!市场价:).)*市场价:<s>(?<p>[\d.]+)(?:(?!麦包价:).)*麦包价:<span[^>]*>(?<mp>[\d.]+)</span>");
MatchCollection mc = reg.Matches(yourStr);
foreach(Match m in mc)
{
richTextBox2.Text += m.Groups["url"].Value + "\n";
richTextBox2.Text += m.Groups["img"].Value + "\n";
richTextBox2.Text += m.Groups["p"].Value + "\n";
richTextBox2.Text += m.Groups["mp"].Value + "\n";
}
fengjian_428 2009-11-04
  • 打赏
  • 举报
回复

string s="你的字符串信息。。";
string sPatternLink = "(? <=href=\")[^\"]+ ";
string sLink=Regex.Match(s,sPatternLink ,RegexOptions.IgnoreCase).ToString();
zhouyanfss 2009-11-04
  • 打赏
  • 举报
回复
\"([^\"]+\.(html|htm|asp|aspx|php|jsp|...))\" 这个匹配网址
\"([^\"]+\.(gif|jpg|bmp|...))\"这个匹配图片,后面的省略号是你添加的其他后缀,具体你可以无限添加
xfreyes 2009-11-04
  • 打赏
  • 举报
回复
这个有办法用正则判断吗?还不如去标签写js判断呢
snowbirdfly 2009-11-04
  • 打赏
  • 举报
回复
看了,不是很明白,程序测试不知道这里面的Regex re = new Regex(@...)怎么写,谢谢~~~
fengjian_428 2009-11-04
  • 打赏
  • 举报
回复
(href链接网址):(?<=href=")[^"]+
(图片地址) :(?<=src=")[^"]+
(?<=市场价:<s>)\d+\.?\d*
(?<=麦包价:<span class="price">)\d+\.?\d*

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧