求一正则表达式

whzdp 2009-04-10 03:00:12
<table width="100%" cellpadding="0" cellspacing="1" class="grid">
<tr align="center">
<td width="20%" class="head">文章名称</td>
<td width="40%" class="head">最新章节</td>
<td width="15%" class="head">作者</td>
<td width="9%" class="head">字数</td>
<td width="10%" class="head">更新</td>
<td width="6%" class="head">状态</td>
</tr>
<tr>
<td class="odd"><a href="http://www.33sy.com/shuyuan_info/30/30355.htm">网王同人之三生有幸</a></td>
<td class="even"><a href="http://www.33sy.com/files/article/book/30/30355/index.html" target="_blank">正文 第一卷 第九章</a></td>
<td class="odd">嘟小離</td>
<td class="even">17K</td>
<td class="odd" align="center">09-04-10</td>
<td class="even" align="center">连载</td>
</tr>
<tr>
<td class="odd"><a href="http://www.33sy.com/shuyuan_info/29/29197.htm">红楼奇梦</a></td>
<td class="even"><a href="http://www.33sy.com/files/article/book/29/29197/index.html" target="_blank">正文 四十七章</a></td>
<td class="odd">江南鱼翁</td>
<td class="even">93K</td>
<td class="odd" align="center">09-04-10</td>
<td class="even" align="center">连载</td>
</tr>
<tr>
<td class="odd"><a href="http://www.33sy.com/shuyuan_info/30/30539.htm">《狐狸,你在干嘛》</a></td>
<td class="even"><a href="http://www.33sy.com/files/article/book/30/30539/index.html" target="_blank">正文 第九章 谁是小狗</a></td>
<td class="odd">笨笨菩提</td>
<td class="even">22K</td>
<td class="odd" align="center">09-04-10</td>
<td class="even" align="center">连载</td>
</tr>
<tr>
<td class="odd"><a href="http://www.33sy.com/shuyuan_info/28/28610.htm"><<擦肩而过>></a></td>
<td class="even"><a href="http://www.33sy.com/files/article/book/28/28610/index.html" target="_blank">正文 第四十二章 道歉</a></td>
<td class="odd">kamzxj</td>
<td class="even">91K</td>
<td class="odd" align="center">09-04-10</td>
<td class="even" align="center">连载</td>
</tr>
<tr>
<td class="odd"><a href="http://www.33sy.com/shuyuan_info/31/31162.htm">绫罗绸缎</a></td>
<td class="even"><a href="http://www.33sy.com/files/article/book/31/31162/index.html" target="_blank">正文 民女陌然</a></td>
<td class="odd">月千寒</td>
<td class="even">3K</td>
<td class="odd" align="center">09-04-10</td>
<td class="even" align="center">连载</td>
</tr>
<tr>
<td class="odd"><a href="http://www.33sy.com/shuyuan_info/29/29745.htm">倾世红颜</a></td>
<td class="even"><a href="http://www.33sy.com/files/article/book/29/29745/index.html" target="_blank">正文 【王显献计策】1</a></td>
<td class="odd">玉宫</td>
<td class="even">128K</td>
<td class="odd" align="center">09-04-10</td>
<td class="even" align="center">连载</td>
</tr>
<tr>
<td class="odd"><a href="http://www.33sy.com/shuyuan_info/29/29552.htm">凤凰错</a></td>
<td class="even"><a href="http://www.33sy.com/files/article/book/29/29552/index.html" target="_blank">正文 0049</a></td>
<td class="odd">雨痕愁雾</td>
<td class="even">95K</td>
<td class="odd" align="center">09-04-10</td>
<td class="even" align="center">连载</td>
</tr>
<tr>
<td class="odd"><a href="http://www.33sy.com/shuyuan_info/29/29947.htm">绝情龙帝的小七宠妃</a></td>
<td class="even"><a href="http://www.33sy.com/files/article/book/29/29947/index.html" target="_blank">正文 第042章</a></td>
<td class="odd">刷新的墙壁</td>
<td class="even">73K</td>
<td class="odd" align="center">09-04-10</td>
<td class="even" align="center">连载</td>
</tr>
<tr>
<td class="odd"><a href="http://www.33sy.com/shuyuan_info/30/30429.htm">双猫传奇</a></td>
<td class="even"><a href="http://www.33sy.com/files/article/book/30/30429/index.html" target="_blank">正文 第十章 谁最温柔?</a></td>
<td class="odd">猫的温柔</td>
<td class="even">40K</td>
<td class="odd" align="center">09-04-10</td>
<td class="even" align="center">连载</td>
</tr>

</table>






我要滤出每一个表格的第一列网址以上表格中有10条附合,但不知道表达式怎么写
<td class="odd"><a href="http://www.33sy.com/shuyuan_info/30/30355.htm">网王同人之三生有幸</a></td>就是将每一行的第一列的网址滤出来如上,要的结果是http://www.33sy.com/shuyuan_info/30/30355.htm




...全文
119 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
whzdp 2009-04-10
  • 打赏
  • 举报
回复
结贴,两位大哥的代码都正确,谢谢!!!
wackyboy 2009-04-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 whzdp 的回复:]
不好意思,是九个,但上面两位的表达式都不行
[/Quote]

Regex re = new Regex("(?is)(?<=<tr[^>]*>\\s*<td[^>]*>\\s*<a.*?href=\")[^\"]+(?=\">)");
Match m = re.Match(input);
while (m.Success)
{
Console.WriteLine(m.Value);
m = m.NextMatch();
}// 我是这样用的 是可以的
bsdekj2 2009-04-10
  • 打赏
  • 举报
回复
一定要正则吗?
我以前一个处理HTTP代码用的.你看看

string[] fileUrl = Exstring.GetInsideStr("代码文件","<td class=\"odd\"> <a href=\"","\">");


/// <summary>
/// 扩展字符串处理
/// </summary>
public class ExString
{
/// <summary>
/// 获取该文字出现的次数
/// </summary>
/// <param name="str">需要查找的主体文档</param>
/// <param name="pointStr">关键文字</param>
/// <returns>次数</returns>
public static int GetStrTime(string str,string pointStr)
{
int result = -1;//统计总数
int nowpoint = 0;//计算当前位置
while (nowpoint != -1)
{
result++;
if (nowpoint == 0)
nowpoint = str.IndexOf(pointStr);
else
nowpoint = str.IndexOf(pointStr, nowpoint + 1);
}
return result;
}

#region 获取某两个字符串里面的所有字符串
/// <summary>
/// 获取某两个字符串里面的所有字符串
/// 前后标志字符不可一样
/// </summary>
/// <param name="str">主体文档</param>
/// <param name="strA">前面的字符串</param>
/// <param name="strB">后面的字符串</param>
/// <returns>夹在中间的字符串集</returns>
public static string[] GetInsideStr(string str, string strA, string strB)
{
int A = 0, B = 0;
int AL = strA.Length;
int BL = strB.Length;
System.Collections.ArrayList forsave = new System.Collections.ArrayList();
while (true)
{
if (B == 0)
A = str.IndexOf(strA);
else
A = str.IndexOf(strA, B + BL);//从上个B结尾处开始找A
if (A != -1)//有可能A为-1
{
B = str.IndexOf(strB, A + AL);//从上面的A后面开始找B
if (B != -1)
{
A = str.LastIndexOf(strA, B);//从B往前找A 位置确定AB间
forsave.Add(str.Substring(A + AL, B - A - AL));
}
else
break;
}
else
break;
}
string[] result = (string[])forsave.ToArray(typeof(string));
return result;
}
/// <summary>
/// 获取某两个字符串里面的所有字符串
/// 前后标志字符不可一样
/// </summary>
/// <param name="str">主体文档</param>
/// <param name="strA">前面的字符串</param>
/// <param name="offsetA">前面的偏移位置数(往右)</param>
/// <param name="strB">后面的字符串</param>
/// <param name="offsetB">后面的偏移位置数(往右)</param>
/// <returns>夹在中间的字符串集</returns>
public static string[] GetInsideStr(string str, string strA, int offsetA, string strB, int offsetB)
{
int A = 0, B = 0;
int AL = strA.Length + offsetA;
int BL = strB.Length + offsetB;
System.Collections.ArrayList forsave = new System.Collections.ArrayList();
while (true)
{
if (B == 0)
A = str.IndexOf(strA);
else
A = str.IndexOf(strA, B + BL);//从上个B结尾处开始找A
if (A != -1)//有可能A为-1
{
B = str.IndexOf(strB, A + AL);//从上面的A后面开始找B
if (B != -1)
{
A = str.LastIndexOf(strA, B);//从B往前找A 位置确定AB间
forsave.Add(str.Substring(A + AL, B - A - AL));
}
else
break;
}
else
break;
}
string[] result = (string[])forsave.ToArray(typeof(string));
return result;
}
/// <summary>
/// 获取某两个字符串里面的所有字符串
/// 前后标志字符不可一样
/// </summary>
/// <param name="getindex">获取第几个匹配的字符(无则返回null)</param>
/// <param name="str">主体文档</param>
/// <param name="strA">前面的字符串</param>
/// <param name="strB">后面的字符串</param>
/// <returns>夹在中间的字符串集</returns>
public static string GetInsideStr(int getindex,string str, string strA, string strB)
{
int A = 0, B = 0;
int AL = strA.Length;
int BL = strB.Length;
System.Collections.ArrayList forsave = new System.Collections.ArrayList();
while (true)
{
if (B == 0)
A = str.IndexOf(strA);
else
A = str.IndexOf(strA, B + BL);//从上个B结尾处开始找A
if (A != -1)//有可能A为-1
{
B = str.IndexOf(strB, A + AL);//从上面的A后面开始找B
if (B != -1)
{
A = str.LastIndexOf(strA, B);//从B往前找A 位置确定AB间
forsave.Add(str.Substring(A + AL, B - A - AL));
}
else
break;
}
else
break;
}
string[] result = (string[])forsave.ToArray(typeof(string));
try
{
return result[getindex - 1];
}
catch
{
return null;
}
}
/// <summary>
/// 获取某两个字符串里面的所有字符串
/// 前后标志字符不可一样
/// </summary>
/// <param name="str">主体文档</param>
/// <param name="getindex">获取第几个匹配的字符(无则返回null)</param>
/// <param name="strA">前面的字符串</param>
/// <param name="offsetA">前面的偏移位置数(往右)</param>
/// <param name="strB">后面的字符串</param>
/// <param name="offsetB">后面的偏移位置数(往右)</param>
/// <returns>夹在中间的字符串集</returns>
public static string GetInsideStr(int getindex, string str, string strA
, int offsetA, string strB, int offsetB)
{
int A = 0, B = 0;
int AL = strA.Length + offsetA;
int BL = strB.Length + offsetB;
System.Collections.ArrayList forsave = new System.Collections.ArrayList();
while (true)
{
if (B == 0)
A = str.IndexOf(strA);
else
A = str.IndexOf(strA, B + BL);//从上个B结尾处开始找A
if (A != -1)//有可能A为-1
{
B = str.IndexOf(strB, A + AL);//从上面的A后面开始找B
if (B != -1)
{
A = str.LastIndexOf(strA, B);//从B往前找A 位置确定AB间
forsave.Add(str.Substring(A + AL, B - A - AL));
}
else
break;
}
else
break;
}
string[] result = (string[])forsave.ToArray(typeof(string));
try
{
return result[getindex - 1];
}
catch
{
return null;
}
}
#endregion
}
whzdp 2009-04-10
  • 打赏
  • 举报
回复
不好意思,是九个,但上面两位的表达式都不行
wackyboy 2009-04-10
  • 打赏
  • 举报
回复

"(?is)(?<=<tr[^>]*>\\s*<td[^>]*>\\s*<a.*?href=\")[^\"]+(?=\">)"
// 应该有九个吧

wackyboy 2009-04-10
  • 打赏
  • 举报
回复

"(?<=<tr[^>]*>\\s*<td[^>]*>\\s*<a.*?href=\")[^\"]+(?=\">)" // 应该有九个吧

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧