正则 ,很迷糊啊怎么只匹配一次
正则 ,很迷糊啊,如何只匹配一次
<div class="latest-list">.*?</div>
我只想得到第一组符合要求的内容,可是这条正则匹配出了8个相同的,如何只让它匹配一次就可以了
搞了好久都不行,要么没有,要么就是8条,我只要第一条
------解决方案--------------------
匹配了8个结果
只需要第一次的就行了阿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace vs_2010_test_pass
{
class Program
{
static void Main(string[] args)
{
string text = @"<td class=bgColor>女</td><td>冯金</td><td colspan=2>222224197112222202</td><td>1972-12-22</td><td class=bgColor>男</td><td>张三</td><td colspan=2>222224197212222202</td><td>1972-12-22</td>";
string rex = @"\d{15}
------解决方案--------------------
\d{18}";
Regex r = new Regex(rex, RegexOptions.IgnoreCase);
MatchCollection m = r.Matches(text);
for (int i = 0; i < m.Count; i++)
{
Match match = m[i];
Console.WriteLine("Match[{0}]========================", i);
for (int j = 0; j < match.Groups.Count; j++)
{
if(j == 0)
//这就是第一次的结果啊,很明显 if 语句
Console.WriteLine("Groups[{0}]={1}", j, match.Groups[j].Value);
}
}
Console.ReadLine();
}
}
}
------解决方案--------------------
用正则表达式提取html中的一个指定div对 参考这个吧。
------解决方案--------------------
------解决方案--------------------
Matches匹配多个
要匹配一个,就调用Match就可以了。
<div class="latest-list">.*?</div>
我只想得到第一组符合要求的内容,可是这条正则匹配出了8个相同的,如何只让它匹配一次就可以了
搞了好久都不行,要么没有,要么就是8条,我只要第一条
------解决方案--------------------
匹配了8个结果
只需要第一次的就行了阿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace vs_2010_test_pass
{
class Program
{
static void Main(string[] args)
{
string text = @"<td class=bgColor>女</td><td>冯金</td><td colspan=2>222224197112222202</td><td>1972-12-22</td><td class=bgColor>男</td><td>张三</td><td colspan=2>222224197212222202</td><td>1972-12-22</td>";
string rex = @"\d{15}
------解决方案--------------------
\d{18}";
Regex r = new Regex(rex, RegexOptions.IgnoreCase);
MatchCollection m = r.Matches(text);
for (int i = 0; i < m.Count; i++)
{
Match match = m[i];
Console.WriteLine("Match[{0}]========================", i);
for (int j = 0; j < match.Groups.Count; j++)
{
if(j == 0)
//这就是第一次的结果啊,很明显 if 语句
Console.WriteLine("Groups[{0}]={1}", j, match.Groups[j].Value);
}
}
Console.ReadLine();
}
}
}
------解决方案--------------------
用正则表达式提取html中的一个指定div对 参考这个吧。
------解决方案--------------------
string text = @"<td class=bgColor>女</td><td>冯金</td><td colspan=2>222224197112222202</td><td>1972-12-22</td><td class=bgColor>男</td><td>张三</td><td colspan=2>222224197212222202</td><td>1972-12-22</td>";
string rex = @"\d{15}
------解决方案--------------------
\d{18}";
Regex r = new Regex(rex, RegexOptions.IgnoreCase);
Match m = r.Matches(text)[0]; //在这里小改一下就可以了
------解决方案--------------------
Matches匹配多个
要匹配一个,就调用Match就可以了。