求正则表达式

yrf741676206 2011-11-01 01:11:59

<!--bof Additional Product Images -->

<div id="productAdditionalImages">
<p>The photos are taken from actual items!</p>
<div id="gallery">

<div class="centeredContent liubobox" style=" float:left;width:33%;">
<a href="images/lv/M93448_01.png" rel="lightbox[roadtrip]"><img src="bmz_cache/8/88b52691f01822a5ffbdb1af7c770957.image.64x80.png" alt="Louis Vuitton Monogram Empreinte Artsy MM M93448" title=" Louis Vuitton Monogram Empreinte Artsy MM M93448 " width="64" height="80" style="position:relative" onmouseover="showtrail('bmz_cache/8/88b52691f01822a5ffbdb1af7c770957.image.300x375.png','Louis Vuitton Monogram Empreinte Artsy MM M93448',64,80,300,375,this,0,0,64,80);" onmouseout="hidetrail();" /><br /></a>
<noscript><a href="http://www.stylebags.net/index.php?main_page=popup_image_additional&pID=710&pic=0&products_image_large_additional=images/lv/M93448_01.png" target="_blank"><img src="bmz_cache/8/88b52691f01822a5ffbdb1af7c770957.image.64x80.png" alt="Louis Vuitton Monogram Empreinte Artsy MM M93448" title=" Louis Vuitton Monogram Empreinte Artsy MM M93448 " width="64" height="80" style="position:relative" onmouseover="showtrail('bmz_cache/8/88b52691f01822a5ffbdb1af7c770957.image.300x375.png','Louis Vuitton Monogram Empreinte Artsy MM M93448',64,80,300,375,this,0,0,64,80);" onmouseout="hidetrail();" /><br /></a></noscript></div>
<div class="centeredContent liubobox" style=" float:left;width:33%;">
<a href="images/lv/M93448_02.png" rel="lightbox[roadtrip]"><img src="bmz_cache/3/38fe8117de12d8f9c17f407dbbf95e0e.image.64x80.png" alt="Louis Vuitton Monogram Empreinte Artsy MM M93448" title=" Louis Vuitton Monogram Empreinte Artsy MM M93448 " width="64" height="80" style="position:relative" onmouseover="showtrail('bmz_cache/3/38fe8117de12d8f9c17f407dbbf95e0e.image.300x375.png','Louis Vuitton Monogram Empreinte Artsy MM M93448',64,80,300,375,this,0,0,64,80);" onmouseout="hidetrail();" /><br /></a>
<noscript><a href="http://www.stylebags.net/index.php?main_page=popup_image_additional&pID=710&pic=1&products_image_large_additional=images/lv/M93448_02.png" target="_blank"><img src="bmz_cache/3/38fe8117de12d8f9c17f407dbbf95e0e.image.64x80.png" alt="Louis Vuitton Monogram Empreinte Artsy MM M93448" title=" Louis Vuitton Monogram Empreinte Artsy MM M93448 " width="64" height="80" style="position:relative" onmouseover="showtrail('bmz_cache/3/38fe8117de12d8f9c17f407dbbf95e0e.image.300x375.png','Louis Vuitton Monogram Empreinte Artsy MM M93448',64,80,300,375,this,0,0,64,80);" onmouseout="hidetrail();" /><br /></a></noscript></div>
<div class="centeredContent liubobox" style=" float:left;width:33%;">
<a href="images/lv/M93448_03.png" rel="lightbox[roadtrip]"><img src="bmz_cache/8/812460991d65c719e3b04127e2523d5c.image.64x80.png" alt="Louis Vuitton Monogram Empreinte Artsy MM M93448" title=" Louis Vuitton Monogram Empreinte Artsy MM M93448 " width="64" height="80" style="position:relative" onmouseover="showtrail('bmz_cache/8/812460991d65c719e3b04127e2523d5c.image.300x375.png','Louis Vuitton Monogram Empreinte Artsy MM M93448',64,80,300,375,this,0,0,64,80);" onmouseout="hidetrail();" /><br /></a>
<noscript><a href="http://www.stylebags.net/index.php?main_page=popup_image_additional&pID=710&pic=2&products_image_large_additional=images/lv/M93448_03.png" target="_blank"><img src="bmz_cache/8/812460991d65c719e3b04127e2523d5c.image.64x80.png" alt="Louis Vuitton Monogram Empreinte Artsy MM M93448" title=" Louis Vuitton Monogram Empreinte Artsy MM M93448 " width="64" height="80" style="position:relative" onmouseover="showtrail('bmz_cache/8/812460991d65c719e3b04127e2523d5c.image.300x375.png','Louis Vuitton Monogram Empreinte Artsy MM M93448',64,80,300,375,this,0,0,64,80);" onmouseout="hidetrail();" /><br /></a></noscript></div>
<br class="clearBoth" />

</div>
</div>



<!--eof Additional Product Images -->


以上是字符串
取出其中的
M93448_01.png
http://www.stylebags.net/index.php?main_page=popup_image_additional&pID=710&pic=0&products_image_large_additional=images/lv/M93448_01.png

M93448_02.png
http://www.stylebags.net/index.php?main_page=popup_image_additional&pID=710&pic=1&products_image_large_additional=images/lv/M93448_02.png

M93448_03.png
http://www.stylebags.net/index.php?main_page=popup_image_additional&pID=710&pic=2&products_image_large_additional=images/lv/M93448_03.png

这些有可能变 但是固定格式不变比如M93448_01.png 和M93448_02.png都在一个文件夹下 但是名字可能不同

迎请大神
...全文
158 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
yrf741676206 2011-11-01
  • 打赏
  • 举报
回复
OK 非常感谢各位大神

结贴
gxmark 2011-11-01
  • 打赏
  • 举报
回复
帖子和火爆呀!
诺维斯基 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 huangwenquan123 的回复:]
引用 16 楼 soonfei 的回复:
引用 15 楼 soonfei 的回复:
刚才,你那个帖子,我给你的,不能用?



C# code
Regex reg = new Regex(@"(?is)<(h1|div|h2)[^>]*?>(?:[^::]+?[::])?(.+?)</\1>");

foreach (Match m in reg.Matches(str))
{……
[/Quote]
嗯,是的,比如:<h1>中还有其他的标签,就不行了。
不过,这需求是充满变数的。
万能的,不存在。
只能是楼主尽量写清楚需求,我们尽量的可扩展。
yrf741676206 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 soonfei 的回复:]
引用 15 楼 soonfei 的回复:
刚才,你那个帖子,我给你的,不能用?



C# code

Regex reg = new Regex(@"(?is)<(h1|div|h2)[^>]*?>(?:[^::]+?[::])?(.+?)</\1>");

foreach (Match m in reg.Matches(str))
{
……
[/Quote]
大神你的得到的是很大一串字符串 我对于正则很陌生
黄大师 和
Return_false
的解决方案不错 嘿嘿 马上回来结贴
huangwenquan123 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 soonfei 的回复:]
引用 15 楼 soonfei 的回复:
刚才,你那个帖子,我给你的,不能用?



C# code
Regex reg = new Regex(@"(?is)<(h1|div|h2)[^>]*?>(?:[^::]+?[::])?(.+?)</\1>");

foreach (Match m in reg.Matches(str))
{
……
[/Quote]
其实如果匹配的就是lz给的那堆字符串的话是没问题
不过那堆字符串如果只是其中一部分的话,用h1,div,h2来匹配的话可能会匹配到多余的其他东西。
诺维斯基 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 soonfei 的回复:]
刚才,你那个帖子,我给你的,不能用?
[/Quote]

Regex reg = new Regex(@"(?is)<(h1|div|h2)[^>]*?>(?:[^::]+?[::])?(.+?)</\1>");

foreach (Match m in reg.Matches(str))
{
Response.Write(m.Groups[2].Value+"<br/>");
}
//稍微变一下,就可以了
诺维斯基 2011-11-01
  • 打赏
  • 举报
回复
刚才,你那个帖子,我给你的,不能用?
huangwenquan123 2011-11-01
  • 打赏
  • 举报
回复
            string str = @"<!--bof Product Name-->

<h1 id=""productName"" class=""productGeneral"">2010 New LV Monogram Canvas Neverfull MM M98789</h1>

<!--eof Product Name-->

<div id=""stylebags_pro_model"">



Model: M98789</div>



<!--bof Product Price block -->

<h2 id=""productPrices"" class=""productGeneral"">

$229.00</h2>";
Regex reg = new Regex(@"(?is)(?<=<h1[^>]*?id=""productName""[^>]*?>(?:(?!</h2).)*)(?:[^><:]+:)?\s*([^><]+?)\s*<[^>]*?>");
foreach (Match m in reg.Matches(str))
Console.WriteLine(m.Groups[1].Value);
  • 打赏
  • 举报
回复

StreamReader sr = new StreamReader(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));
string tempStr =sr.ReadToEnd();
string pattern = @"(?m)(?<=<(h1|div|h2)[^>]*>)([^<]+:)*([^<]+)(?=</?\1>)";
MatchCollection mc = Regex.Matches(tempStr, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
foreach (Match m in mc)
{

string text = m.Groups[3].Value;//循环输出


}


大哥,不要连载了,伤不起呀,呵呵
yrf741676206 2011-11-01
  • 打赏
  • 举报
回复
哦 原来如此 嘿嘿 大神就是大神
现在有个新情况也是最后一个情况了 哦也
eof Main Product Image-->



<!--bof Product Name-->

<h1 id="productName" class="productGeneral">2010 New LV Monogram Canvas Neverfull MM M98789</h1>

<!--eof Product Name-->

<div id="stylebags_pro_model">



Model: M98789</div>



<!--bof Product Price block -->

<h2 id="productPrices" class="productGeneral">

$229.00</h2>

<!--
获取其中的
2010 New LV Monogram Canvas Neverfull MM M98789
M98789
$229.00
怎么搞呢
huangwenquan123 2011-11-01
  • 打赏
  • 举报
回复
            string str = File.ReadAllText(@"E:\1.txt", Encoding.GetEncoding("gb2312"));
Regex reg = new Regex(@"(?is)(?<!<noscript>\s*)<a[^>]*?href=""([^""]+)""[^>]*?>");
foreach (Match m in reg.Matches(str))
{
Console.WriteLine(m.Groups[1].Value);
}
huangwenquan123 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yrf741676206 的回复:]
黄大师你的得不到 数据
可能是我哪儿没弄对吧
Return_false大神的对了

现在只获取
images/lv/XX.png怎么搞呢
[/Quote]咦 我测试没问题啊
  • 打赏
  • 举报
回复
注释好像错了
  • 打赏
  • 举报
回复

StreamReader sr = new StreamReader(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));
string tempStr =sr.ReadToEnd();
string pattern = @"(?im)<a[^>]*href=['""]([^'""]+)['""][^>]*rel[^>]*>[\s\S]*?</a>";
MatchCollection mc = Regex.Matches(tempStr, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
foreach (Match m in mc)
{

string png = m.Groups[1].Value;//M93448_01.png


}
yrf741676206 2011-11-01
  • 打赏
  • 举报
回复
黄大师你的得不到 数据
可能是我哪儿没弄对吧
Return_false大神的对了

现在只获取
images/lv/XX.png怎么搞呢
huangwenquan123 2011-11-01
  • 打赏
  • 举报
回复
改了下,原来长链接中就有你要的那个图片名称
            string str = File.ReadAllText(@"E:\1.txt", Encoding.GetEncoding("gb2312"));
Regex reg = new Regex(@"(?is)(?<=<div[^>]*?class=""centeredContent\s*?liubobox""[^>]*?>(?:(?!</?div).)*)<noscript>\s*<a[^>]*?href=""([^""]+/([^""]+))""[^>]*?>");
foreach (Match m in reg.Matches(str))
{
Console.WriteLine(m.Groups[2].Value);
Console.WriteLine(m.Groups[1].Value);
}
huangwenquan123 2011-11-01
  • 打赏
  • 举报
回复
            string str = File.ReadAllText(@"E:\1.txt", Encoding.GetEncoding("gb2312"));
Regex reg = new Regex(@"(?is)(?<=<div[^>]*?class=""centeredContent\s*?liubobox""[^>]*?>(?:(?!</?div).)*)(?:<noscript>\s*<a[^>]*?href=""([^""]+)""[^>]*?>|<a[^>]*?href="".*?(M\d{5}[^""]+)""[^>]*?>)");
foreach (Match m in reg.Matches(str))
Console.WriteLine(m.Groups[1].Value.Length == 0 ? m.Groups[2].Value : m.Groups[1].Value);
  • 打赏
  • 举报
回复

StreamReader sr = new StreamReader(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));
string tempStr =sr.ReadToEnd();
string pattern = @"(?im)<noscript><a[^>]*href=['""]([^'""]+/([^'""]+.[^'""\s]+))['""][^>]*>[\s\S]*?</a>";
MatchCollection mc = Regex.Matches(tempStr, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
foreach (Match m in mc)
{

string png = m.Groups[2].Value;//M93448_01.png
string href = m.Groups[1].Value;//长连接

}
编程有钱人了 2011-11-01
  • 打赏
  • 举报
回复

(?<=lv/)(\w+)
yrf741676206 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 huangwenquan123 的回复:]
C# code


string str = File.ReadAllText(@"E:\1.txt", Encoding.GetEncoding("gb2312"));
Regex reg = new Regex(@"(?is)(?<=<div[^>]*?class=""centeredContent liubobox""[^>]*?>(……
[/Quote]
黄大师
链接对了
但是那个图片只需要images/lv/的后面部分 也就是images/lv/这个不要 只要XX.png
加载更多回复(1)

110,545

社区成员

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

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

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