110,536
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
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*/
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
*/
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;//循环输出内容
}
// 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);