谢谢min_jie,谢谢大家 我把几位的综合了一下 Regex reg = new Regex("(?<=<div [^>]*>)((?!品种代码)[^<])+(?=</div>)", RegexOptions.IgnoreCase); Match mat = reg.Match(str); while (mat.Success) { Console.WriteLine(mat.Value.ToString()); mat = reg.Match(str, mat.Index + mat.Length); } 只列出了第一行就完了,请继续指点
Regex reg = new Regex("(? <= <div [^>]*>)((?!品种代码)[^ <])+(?= </div>)", RegexOptions.IgnoreCase); Match mat = reg.Match(str); while (mat.Success) { Console.WriteLine(mat.Value.ToString()); mat = reg.Match(str, mat.Index + mat.Length); //这句是要干什么? } 可以这样写: Regex reg = new Regex("(? <= <div [^>]*>)((?!品种代码)[^ <])+(?= </div>)", RegexOptions.IgnoreCase); foreach(Match m in reg.Matchs(str)) { Console.WriteLine(m.Value); }
要提取哪些内容也写一下。。
Regex reg = new Regex(@"\<td.*?\>.+?\<\/td\>", RegexOptions.IgnoreCase);
Match mat = reg.Match(str);
while(mat.Success)
{
Response.Write(mat.Value);
mat = reg.Match(str, mat.Index+mat.Length);
}
"<table width=\"98%\" border=\"1\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#999999\">\r\n <tr bgcolor=\"#CCCCCC\">\r\n <td width=\"10%\" height=\"30\">\r\n <div align=\"center\">品种代码</div></td>\r\n <td width=\"10%\" height=\"30\">\r\n <div align=\"center\">a</div></td>\r\n <td width=\"10%\" height=\"30\">\r\n <div align=\"center\">b</div></td>\r\n <td width=\"10%\" height=\"30\">\r\n <div align=\"center\">c</div></td>\r\n <td width=\"10%\" height=\"30\">\r\n <div align=\"center\">d</div></td>\r\n <td width=\"10%\" height=\"30\">\r\n <div align=\"center\">e</div></td>\r\n <td width=\"10%\" height=\"30\">\r\n <div align=\"center\">f</div></td>\r\n <td width=\"10%\" height=\"30\">\r\n <div align=\"center\">g</div></td>\r\n <td width=\"10%\" height=\"30\" align=\"center\">\r\n <div align=\"center\"> h</div></td>\r\n <td width=\"10%\" height=\"30\">\r\n <div align=\"center\"> i </div></td>\r\n </tr>\r\n <tr bgcolor=\"#FFFFFF\">\r\n <td height=\"25\" width=\"10%\" align=\"center\">00001</td>\r\n <td height=\"25\" width=\"10%\" align=\"center\">3080</td>\r\n <td height=\"25\" align=\"center\" width=\"10%
我是想把每一个品种对应的值取出来放在数据库里面
(?<=<div [^>]*>)((?!品种代码)[^<])+(?=</div>)
Regex reg = new Regex("(?<=<div [^>]*>)((?!品种代码)[^<])+(?=</div>)", RegexOptions.IgnoreCase);
Match mat = reg.Match(str);
while (mat.Success)
{
Console.WriteLine(mat.Value.ToString());
mat = reg.Match(str, mat.Index + mat.Length);
}
只列出了第一行就完了,请继续指点
Match mat = reg.Match(str);
while (mat.Success)
{
Console.WriteLine(mat.Value.ToString());
mat = reg.Match(str, mat.Index + mat.Length); //这句是要干什么?
} 可以这样写:
Regex reg = new Regex("(? <= <div [^>]*>)((?!品种代码)[^ <])+(?= </div>)", RegexOptions.IgnoreCase);
foreach(Match m in reg.Matchs(str))
{
Console.WriteLine(m.Value);
}
你还是要把具体的网页代码发出来啊。。要不谁能知道都有哪些列,都有那些规则呢?
如果是很多列,建议用捕获组来实现。<div [^>]*>(?<type>[^<]+)</div>[\s\S]*?<div [^>]*>(?<name>[^<]+)</div>
类似这样的。。
foreach(Match m in reg.Match(str))
{
Console.WriteLine(m.Value);
}
运行错误 2
“System.Text.RegularExpressions.Match”不包含“GetEnumerator”的公共定义,因此 foreach 语句不能作用于“System.Text.RegularExpressions.Match”类型的变量 F:\dotnet\ocs\test\test\test\Form1.cs 190 17 test
刚开始学习C#...
reg.Matches(str)
这回对了。。
CSDN的<号总是会自动在前面加一个空格,注意去掉那个空格。。
Regex reg = new Regex("(? <= <div [^>]*>)((?!品种代码)[^ <])+(?= </div>)", RegexOptions.IgnoreCase);
foreach(Match m in reg.Match(str))
{
Console.WriteLine(m.Value);
}
表头每一列实际是长的中文,我把他简化成了a,b,c...不知道有没有影响到各位专家的判断
a
b
c
d
e
f
g
h
i
有问题吗?
如果是还有其他的列,那正则表达式肯定要把其他列也给加上。。
a b c d e f g
01 4 5 6 7 8 9
02 4 4 ......
我的意思可能没表达清楚,其实是想取出01对应4 5 6 7 8 9 的值 02对应的4 4 等每一个td的值
上面的代码只是把表头abcdefg 取出来
行列是固定的
我对正则表达式基本上不懂,谢谢 min_jie 的耐心
网址是http://www.smm.com.cn/lme.php,table我已经存在string str里面了,想把里面几种金属对应的数据存在数据库里
再次谢谢min_jie
Regex reg=new Regex(pattern);
foreach(Match m in reg.Matches(str))
{
Console.WriteLine(m.Group["type"].Value);
Console.WriteLine(m.Group["open"].Value);
Console.WriteLine(m.Group["high"].Value);
Console.WriteLine(m.Group["low"].Value);
Console.WriteLine(m.Group["close"].Value);
Console.WriteLine(m.Group["nowprice"].Value);
Console.WriteLine(m.Group["balance"].Value);
Console.WriteLine(m.Group["trade"].Value);
Console.WriteLine(m.Group["count"].Value);
Console.WriteLine(m.Group["addcount"].Value);
}
还没能看懂 要好好学学正则了