日期:2014-05-16  浏览次数:20792 次

请问,为什么提取到的都是空值呢,正则写错了吗?
string s = ...
<td row="1" width="33.3%" class="" style="cursor:pointer" >5510</td>
<td row="1" width="33.3%" class="" style="cursor:pointer" >1060</td>
...;

var matches = Regex.Matches(s, "(?<=.*pointer.*>).{4,5}(?=.*</td)");
String[] ss = new String[7];
int j = 0;
foreach (Match item in matches)
{
     ss[j] = item.ToString().Trim(); j++;
}

提取5510等几个数字,然后打印
for (int i = 0; i < ss.Length; i++) { Trace.WriteLine(ss[i]); }

可是打印出来的全是空值,为什么啊?
------解决方案--------------------
"(?<=\.\*pointer\.\*>)[\d]+(?=</td>)"
------解决方案--------------------
引用:
string s = ...
<td row="1" width="33.3%" class="" style="cursor:pointer" >5510</td>
<td row="1" width="33.3%" class="" style="cursor:pointer" >1060</td>
...;

var matches = Regex.Matches(s, "(?<=.*pointer.*>).{4,5}(?=.*</td)");

var matches = Regex.Matches(s, "(?<=.*?pointer[^>]*?>)[^<>]{4,5}(?=</td)");
------解决方案--------------------
(?<=pointer[^>]+>)[^<]{4,5}(?=</td)

如果要匹配的数字一定是整数也可以把[^<]{4,5}换成\d{4,5}
------解决方案--------------------
别搞的太复杂,简单才是王道
>(\d*?)<