<div id="ww" class="xx">
<h1><a class="aacc" href="http://www.xxyy.com/abc/1234.html">今天天气:</a>晴转多云<span>有雨</span></h1>
<div class="yy">
<div class="zz">
<div class="ee"></div>
<p class="dd"><strong>101</strong>
<span><a href="tst1.html"><img src="http://www.xx.com/123.gif" /></a></span> </p>
</div>
<table class="sadf">
<tr>
<th>温度</th>
<th>湿度</th>
<th>紫外线</th>
</tr>
<tr>
<td>102</td>
<td>11</td>
<td>22</td>
</tr>
</table>
<div class="www" id="xxx"><h3>当前时间</h3><div class="asdf"><ul id="asd"><li><span>9</span>点</li><li><span>15</span>分</li><li><span>3</span>秒</li></ul></div></div><div class="aa" id="bb"><p class="cc"><strong>33</strong> 省</p><p class="ee">市区</p><p class="ff">局部地区<br />无大碍</p><p class="zz">特别注意:<strong>无</strong> 人</p></div> </div>
<div class="ww">
<div class="gg"><img src="http://tup.com/abc/yaohuoqudetupian.jpg"/></div>
<div class="afd"><br />不容易啊</div>
</div>
</div>-----------------------
上面代码中,我用c#正则获取以下数据:
1:http://www.xxyy.com/abc/1234.html
2:晴转多云<span>有雨</span>
3:<strong>101</strong>中的101这个数字
4:<td>102</td>中的102这个数字
5:<ul id="asd"><li><span>9</span>点</li><li><span>15</span>分</li><li><span>3</span>秒</li></ul>中的9,15,3这3个数字
6:<img src="http://tup.com/abc/yaohuoqudetupian.jpg"/>中的http://tup.com/abc/yaohuoqudetupian.jpg这个图片的链接谢谢高手帮忙,实在是搞不定了
<h1><a class="aacc" href="http://www.xxyy.com/abc/1234.html">今天天气:</a>晴转多云<span>有雨</span></h1>
<div class="yy">
<div class="zz">
<div class="ee"></div>
<p class="dd"><strong>101</strong>
<span><a href="tst1.html"><img src="http://www.xx.com/123.gif" /></a></span> </p>
</div>
<table class="sadf">
<tr>
<th>温度</th>
<th>湿度</th>
<th>紫外线</th>
</tr>
<tr>
<td>102</td>
<td>11</td>
<td>22</td>
</tr>
</table>
<div class="www" id="xxx"><h3>当前时间</h3><div class="asdf"><ul id="asd"><li><span>9</span>点</li><li><span>15</span>分</li><li><span>3</span>秒</li></ul></div></div><div class="aa" id="bb"><p class="cc"><strong>33</strong> 省</p><p class="ee">市区</p><p class="ff">局部地区<br />无大碍</p><p class="zz">特别注意:<strong>无</strong> 人</p></div> </div>
<div class="ww">
<div class="gg"><img src="http://tup.com/abc/yaohuoqudetupian.jpg"/></div>
<div class="afd"><br />不容易啊</div>
</div>
</div>-----------------------
上面代码中,我用c#正则获取以下数据:
1:http://www.xxyy.com/abc/1234.html
2:晴转多云<span>有雨</span>
3:<strong>101</strong>中的101这个数字
4:<td>102</td>中的102这个数字
5:<ul id="asd"><li><span>9</span>点</li><li><span>15</span>分</li><li><span>3</span>秒</li></ul>中的9,15,3这3个数字
6:<img src="http://tup.com/abc/yaohuoqudetupian.jpg"/>中的http://tup.com/abc/yaohuoqudetupian.jpg这个图片的链接谢谢高手帮忙,实在是搞不定了
string pattern2 = "http://([^\\s]+)\".+?span.+?\\[(.+?)\\].+?>(.+?)<";
Regex reg2 = new Regex(pattern2, RegexOptions.IgnoreCase);
MatchCollection mc2 = reg2.Matches(strHTML);
if (mc2.Count > 0)
{
foreach (Match m in mc2)
{
Response.Write(m.Groups[1].Value);
Response.Write(m.Groups[2].Value);
Response.Write(m.Groups[3].Value);
}
}类似于这种的获取,谢谢
MatchCollection mc = reg.Matches(yourStr);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups[1].Value + "\n";
richTextBox2.Text += m.Groups[2].Value + "\n";
richTextBox2.Text += m.Groups[3].Value + "\n";
richTextBox2.Text += m.Groups[4].Value + "\n";
richTextBox2.Text += m.Groups[5].Value + "\n";
richTextBox2.Text += m.Groups[6].Value + "\n";
richTextBox2.Text += m.Groups[7].Value + "\n";
richTextBox2.Text += m.Groups[8].Value + "\n";
}没优化,看下吧,有不符合的给出具体例子
<tr>
<th>温度</th>
<th>湿度</th>
<th>紫外线</th>
</tr>
如果要获取上面tr代码中的 温度,湿度,紫外线3个值,如果 紫外线 有时候有,有时候没有,请问这个正则应该如何写呢?这样简单了吧,如果有紫外线只获取3个值即可如果没有紫外线,只获取2个值即可
<th>温度</th>
<th>湿度</th>
</tr>
<tr>
<td>102</td>
<td>11</td>
</tr>
";
Regex reg = new Regex(@"(?is)<th>温度</th>\s*<th>湿度</th>.*?<tr>\s*<td>(\d+)</td>\s*<td>(\d+)</td>(?:\s*<td>(\d+)</td>)?");
Match m = reg.Match(test);
if(m.Success)
{
richTextBox2.Text += m.Groups[1].Value + "\n";
richTextBox2.Text += m.Groups[2].Value + "\n";
richTextBox2.Text += m.Groups[3].Value + "\n";
}
<table class="sadf">
<tr>
<th>温度</th>
<th>湿度</th>
<th>紫外线</th>
</tr>
<tr>
<td>102</td>
<td>11</td>
<td>22</td>
</tr>
</table>
<a href="/abc/111.html" target="_blank">你好</a>
<p><a href="/abc/222.html" target="_blank">你好</a></p>前面的湿度,温度,紫外线都获取到了,那么后面有一个<p><a href="/deal/kelemei.html" target="_blank">你好</a></p>,我想获取这个p里面的a的href的内容,但是这个P或许有,或许没有,请问高手如何匹配呢?
<table class="sadf">
<tr>
<th>温度</th>
<th>湿度</th>
<th>紫外线</th>
</tr>
<tr>
<td>102</td>
<td>11</td>
<td>22</td>
</tr>
</table>
<a href="/abc/111.html" target="_blank">你好</a>
<p><a href="/abc/222.html" target="_blank">你好</a></p>前面的湿度,温度,紫外线都获取到了,那么后面有一个<p><a href="/abc/222.html" target="_blank">你好</a></p>,我想获取这个p里面的a的href的内容,但是这个P或许有,或许没有,请问高手如何匹配呢?跟温度,湿度,紫外线一起获取,那么正则需要判断是否包含<p><a href="/abc/222.html" target="_blank">你好</a></p>,如果包含,那么读取a的href,如果不包含,那么不读取
Match m = reg.Match(test);
if(m.Success)
{
richTextBox2.Text += m.Groups[1].Value + "\n";
richTextBox2.Text += m.Groups[2].Value + "\n";
richTextBox2.Text += m.Groups[3].Value + "\n";
richTextBox2.Text += m.Groups[4].Value + "\n";
}