求一正则表达式 在线等

yrf741676206 2011-11-01 10:18:17
从<div id="productDescription" class="productGeneral biggerText"><h3>Description:</h3>
<p>2010 New LV Monogram Canvas Neverfull MM M98789 is a designer bag, if you like it, you could get

one in our online store!</p>
<h3>Features:</h3>
<ul>
<li>Color:As picture</li>
<li>Size: W32 cm X H29 cm X D17 cm</li>
</ul>
<h3>Details:</h3>
<ul>
<li>Side laces to change the shape of the bag</li>
<li>Red textile linning</li>
<li>Leather trimmings</li>
<li>Large interior zipped pocket which can fit a long wallet</li>
<li>Interior D-ring for keys and an accessory<li>
<li>Metallic pieces in shiny golden brass</li>
</ul></div>
这段代码中 获取
2010 New LV Monogram Canvas Neverfull MM M98789 is a designer bag, if you like it, you could get

one in our online store!

As picture

W32 cm X H29 cm X D17 cm

Side laces to change the shape of the bagRed textile linning
Leather trimmings
Large interior zipped pocket which can fit a long wallet
Interior D-ring for keys and an accessory
Metallic pieces in shiny golden brass

跪求大神帮忙
...全文
103 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

StreamReader sr = new StreamReader(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));
string tempStr =sr.ReadToEnd();
string pattern = @"(?m)(?<=<(h1|div|span)[^>]*>)([^<]+:)*([^<]+)(?=</?\1>)";
MatchCollection mc = Regex.Matches(tempStr, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
foreach (Match m in mc)
{
if (!string.IsNullOrEmpty(m.Groups[3].Value))
{
//循环输出内容
Console.WriteLine(m.Groups[3].Value);
}
}
sunny906 2011-11-01
  • 打赏
  • 举报
回复

Regex reg = new Regex(@"(?i)<h1\s*?id=.productName[^>]*?>([^<]*?)</h1>[^;]*?;<span\s*?class=.productSpecialPrice[^>]*?>([^<]*?)</span>");

MatchCollection mc = reg.Matches(test);
String[] number = null;
foreach (Match m in mc)
{
number = m.Groups[1].Value.Split(' ');
Console.WriteLine(m.Groups[1].Value);
Console.WriteLine(number[number.Length-1]);
Console.WriteLine(m.Groups[2].Value);
}

/*输出
Louis Vuitton Monogram Canvas Delightful PM MM M40352
M40352
$209.00*/
诺维斯基 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 yrf741676206 的回复:]
q107770540

大神求解决 最后一个问题了 分不够可以加
[/Quote]
 string str = "<h1 id=\"productName\" class=\"productGeneral\">Louis Vuitton Monogram Canvas Delightful PM MM M40352</h1><!--eof Product Name--><div id=\"stylebags_pro_model\">Model: M40352</div><!--bof Product Price block --><h2 id=\"productPrices\" class=\"productGeneral\"><span class=\"normalprice\">$299.00 </span> <span class=\"productSpecialPrice\">$209.00</span></h2>";

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

foreach (Match m in reg.Matches(str))
{
Response.Write(m.Groups[2].Value+"<br/>");
}
/*
Louis Vuitton Monogram Canvas Delightful PM MM M40352
M40352
$299.00
$209.00
*/
yrf741676206 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 gxmark 的回复:]
string test="<h1 id=\"productName\" class=\"productGeneral\">Louis Vuitton Monogram Canvas Delightful PM MM M40352</h1><!--eof Product Name--><div id=\"stylebags_pro_model\">Model: M40352</div><!--bof……
[/Quote]
没对 没得到值
不过还是谢谢你了

大神 救急啊
gxmark 2011-11-01
  • 打赏
  • 举报
回复
string test="<h1 id=\"productName\" class=\"productGeneral\">Louis Vuitton Monogram Canvas Delightful PM MM M40352</h1><!--eof Product Name--><div id=\"stylebags_pro_model\">Model: M40352</div><!--bof Product Price block --><h2 id=\"productPrices\" class=\"productGeneral\"><span class=\"normalprice\">$299.00 </span> <span class=\"productSpecialPrice\">$209.00</span></h2>";
Regex re = new Regex("<h1 id=\"productName\" class=\"productGeneral\">([^<]*)</h1>.*Model:([^<]*)</div>.*<span class=\"productSpecialPrice\">([^<]*)</span>");
foreach (Match s in re.Matches(test))
{
Console.WriteLine(s.Groups[1].Value.ToString().Trim());
Console.WriteLine(s.Groups[2].Value.ToString().Trim());
Console.WriteLine(s.Groups[3].Value.ToString().Trim());
}
yrf741676206 2011-11-01
  • 打赏
  • 举报
回复
q107770540

大神求解决 最后一个问题了 分不够可以加
yrf741676206 2011-11-01
  • 打赏
  • 举报
回复
还有一个小问题
eof Main Product Image-->



<!--bof Product Name-->

<h1 id="productName" class="productGeneral">Louis Vuitton Monogram Canvas Delightful PM MM M40352</h1>

<!--eof Product Name-->

<div id="stylebags_pro_model">



Model: M40352</div>



<!--bof Product Price block -->

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

<span class="normalprice">$299.00 </span> <span class="productSpecialPrice">$209.00</span></h2>

<!--
这当中获取
Louis Vuitton Monogram Canvas Delightful PM MM M40352

M40352

$209.00
最后一个问题了 拜托大神了
yrf741676206 2011-11-01
  • 打赏
  • 举报
回复
谢谢各位大神 我去试试 解决了很快就回来结贴嘿嘿
q107770540 2011-11-01
  • 打赏
  • 举报
回复

void Main()
{
string str = @"<div id=""productDescription"" class=""productGeneral biggerText""><h3>Description:</h3>
<p>2010 New LV Monogram Canvas Neverfull MM M98789 is a designer bag, if you like it, you could get

one in our online store!</p>
<h3>Features:</h3>
<ul>
<li>Color:As picture</li>
<li>Size: W32 cm X H29 cm X D17 cm</li>
</ul>
<h3>Details:</h3>
<ul>
<li>Side laces to change the shape of the bag</li>
<li>Red textile linning</li>
<li>Leather trimmings</li>
<li>Large interior zipped pocket which can fit a long wallet</li>
<li>Interior D-ring for keys and an accessory<li>
<li>Metallic pieces in shiny golden brass</li>
</ul></div>";
foreach(Match m in Regex.Matches(str,@"(?is)(?<=<(p|li)>).*?(?=</?\1>)"))
{
Console.WriteLine((m.Value.Contains("Color:")||m.Value.Contains("Size:"))?m.Value.Split(':')[1]:m.Value);
}

/*
2010 New LV Monogram Canvas Neverfull MM M98789 is a designer bag, if you like it, you could get

one in our online store!
As picture
W32 cm X H29 cm X D17 cm
Side laces to change the shape of the bag
Red textile linning
Leather trimmings
Large interior zipped pocket which can fit a long wallet
Interior D-ring for keys and an accessory


Metallic pieces in shiny golden brass

*/
}

  • 打赏
  • 举报
回复

StreamReader sr = new StreamReader(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));
string tempStr =sr.ReadToEnd();
string pattern = @"<(li|p)[^>]*>([^<]+:)*([^<]+)</(li|p)>";
MatchCollection mc = Regex.Matches(tempStr, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
foreach (Match m in mc)
{
string tempValue = m.Value;
string text = m.Groups[3].Value;//循环输出内容
}
Flashcom 2011-11-01
  • 打赏
  • 举报
回复
楼上的是正解
Icedmilk 2011-11-01
  • 打赏
  • 举报
回复

// html 就是输入字符串
StringBuilder sb = new StringBuilder();
foreach(Match m in Regex.Matches(html, @"<([^>]+)>([^<]*)</[^>]+>")) {
if(m.Groups[1].Value == "p") {
sb.Append(m.Groups[2].Value);
sb.Append('\n');
} else if(m.Groups[1].Value == "li") {
string value = m.Groups[2].Value;
int idx = value.IndexOf(':');
if(idx != -1) value = value.Substring(idx + 1);
sb.Append(value);
sb.Append('\n');
}
}
Console.WriteLine(sb);
yrf741676206 2011-11-01
  • 打赏
  • 举报
回复
大牛些不要隐身啊
yrf741676206 2011-11-01
  • 打赏
  • 举报
回复
跪求各位兔子当及各位大神帮忙
在线等 解决马上结贴 谢谢了
yrf741676206 2011-11-01
  • 打赏
  • 举报
回复
分太少了
请各位大神 来这里
http://topic.csdn.net/u/20111101/13/7958f503-ec6c-43b4-857a-a62e81039ad7.html
继续接分

110,536

社区成员

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

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

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