求一个正则表达式的写法 ( 积分: 50 )

G

gxch

Unregistered / Unconfirmed
GUEST, unregistred user!
我想用正则表达式找出下面html代码中的所有连接,但是弄了好长时间也没写好,总是达不到需要的效果,我想要他的连接地址和相应的连接信息,请大家帮帮忙吧:



<TD align=left width=&quot;38%&quot;><FONT face=Wingdings>1</FONT><A
href=&quot;ashow.php?order=title&amp;amp;sid=9&quot;><B>文章名</B></A></TD>
<TD width=&quot;8%&quot;><A
href=&quot;ashow.php?order=byte&amp;amp;sid=9&quot;><B>Byte</B></A></TD>
<TD width=&quot;12%&quot;><A
href=&quot;ashow.php?order=author&amp;amp;sid=9&quot;><B>作者</B></A></TD>
<TD width=&quot;11%&quot;><A
href=&quot;ashow.php?order=publish&amp;amp;sid=9&quot;><B>发表时间</B></A></TD>
<TD width=&quot;8%&quot;><A
href=&quot;ashow.php?order=click&amp;amp;sid=9&quot;><B>阅读数</B></A></TD>
<TD width=&quot;8%&quot; colSpan=2><A
href=&quot;ashow.php?order=reply&amp;amp;sid=9&quot;><B>评论数</B></A></TD>
<TD width=&quot;11%&quot;><A
href=ashow.php?order=recent&amp;amp;sid=9><B>最新评论</B></A></TD></TR>
<TR onmouseover=&quot;changeto('dddddd')&quot; onmouseout=&quot;changeback('')&quot;
align=middle>
<TD align=left bgColor=#ffffff><FONT face=Wingdings size=4>2</FONT>
<A
href=letters/show.phtml?aid=2005103021082688
target=_blank>魍魉生(1)</A> </TD>
<TD>31741</TD>
<TD onmouseover=&quot;mOvr('intr1')&quot; onmouseout=&quot;mOut('intr1')&quot;>叶红霜
<DIV id=intr1
style=&quot;VISIBILITY: hidden; POSITION: absolute&quot;></DIV></TD>
<TD>11-01 13:23</TD>
<TD>46</TD>
<TD>0</TD>
<TD>&amp;nbsp;</TD>
<TD>---</TD></TR>
<TR onmouseover=&quot;changeto('dddddd')&quot; onmouseout=&quot;changeback('')&quot;
align=middle>
<TD align=left bgColor=#ffffff><FONT face=Wingdings size=4>2</FONT>
<A
href=letters/show.phtml?aid=2005103023091954
target=_blank>自杀</A> </TD>
<TD>9058</TD>
<TD onmouseover=&quot;mOvr('intr2')&quot; onmouseout=&quot;mOut('intr2')&quot;>舫文萱
<DIV id=intr2
style=&quot;VISIBILITY: hidden; POSITION: absolute&quot;></DIV></TD>
<TD>11-01 13:23</TD>
<TD>90</TD>
<TD>0</TD>
<TD>&amp;nbsp;</TD>
<TD>---</TD></TR>
<TR onmouseover=&quot;changeto('dddddd')&quot; onmouseout=&quot;changeback('')&quot;
align=middle>
<TD align=left bgColor=#ffffff><FONT face=Wingdings size=4>2</FONT>
<A
href=letters/show.phtml?aid=2005103019313399
target=_blank>迷山鬼话</A> </TD>
<TD>6744</TD>
<TD onmouseover=&quot;mOvr('intr3')&quot; onmouseout=&quot;mOut('intr3')&quot;>平生不肖生
<DIV id=intr3
style=&quot;VISIBILITY: hidden; POSITION: absolute&quot;>一曲豪歌济世穷,两鬓萧索禁烟中.平生寥落浮云意,落迹江南不肖生
</DIV></TD>


主要是里面的连接信息中有的含有<B>,有的连接中含有“,有的没有,这样的,还要和不含有的兼容,我不知道该怎么做,写两个表达式也可以,主要是不能重复,第一个表达式匹配出来的第二个表达式中不能再匹配。大家看该怎么写啊?分不够我可以再加。
 
我想用正则表达式找出下面html代码中的所有连接,但是弄了好长时间也没写好,总是达不到需要的效果,我想要他的连接地址和相应的连接信息,请大家帮帮忙吧:



<TD align=left width=&quot;38%&quot;><FONT face=Wingdings>1</FONT><A
href=&quot;ashow.php?order=title&amp;amp;sid=9&quot;><B>文章名</B></A></TD>
<TD width=&quot;8%&quot;><A
href=&quot;ashow.php?order=byte&amp;amp;sid=9&quot;><B>Byte</B></A></TD>
<TD width=&quot;12%&quot;><A
href=&quot;ashow.php?order=author&amp;amp;sid=9&quot;><B>作者</B></A></TD>
<TD width=&quot;11%&quot;><A
href=&quot;ashow.php?order=publish&amp;amp;sid=9&quot;><B>发表时间</B></A></TD>
<TD width=&quot;8%&quot;><A
href=&quot;ashow.php?order=click&amp;amp;sid=9&quot;><B>阅读数</B></A></TD>
<TD width=&quot;8%&quot; colSpan=2><A
href=&quot;ashow.php?order=reply&amp;amp;sid=9&quot;><B>评论数</B></A></TD>
<TD width=&quot;11%&quot;><A
href=ashow.php?order=recent&amp;amp;sid=9><B>最新评论</B></A></TD></TR>
<TR onmouseover=&quot;changeto('dddddd')&quot; onmouseout=&quot;changeback('')&quot;
align=middle>
<TD align=left bgColor=#ffffff><FONT face=Wingdings size=4>2</FONT>
<A
href=letters/show.phtml?aid=2005103021082688
target=_blank>魍魉生(1)</A> </TD>
<TD>31741</TD>
<TD onmouseover=&quot;mOvr('intr1')&quot; onmouseout=&quot;mOut('intr1')&quot;>叶红霜
<DIV id=intr1
style=&quot;VISIBILITY: hidden; POSITION: absolute&quot;></DIV></TD>
<TD>11-01 13:23</TD>
<TD>46</TD>
<TD>0</TD>
<TD>&amp;nbsp;</TD>
<TD>---</TD></TR>
<TR onmouseover=&quot;changeto('dddddd')&quot; onmouseout=&quot;changeback('')&quot;
align=middle>
<TD align=left bgColor=#ffffff><FONT face=Wingdings size=4>2</FONT>
<A
href=letters/show.phtml?aid=2005103023091954
target=_blank>自杀</A> </TD>
<TD>9058</TD>
<TD onmouseover=&quot;mOvr('intr2')&quot; onmouseout=&quot;mOut('intr2')&quot;>舫文萱
<DIV id=intr2
style=&quot;VISIBILITY: hidden; POSITION: absolute&quot;></DIV></TD>
<TD>11-01 13:23</TD>
<TD>90</TD>
<TD>0</TD>
<TD>&amp;nbsp;</TD>
<TD>---</TD></TR>
<TR onmouseover=&quot;changeto('dddddd')&quot; onmouseout=&quot;changeback('')&quot;
align=middle>
<TD align=left bgColor=#ffffff><FONT face=Wingdings size=4>2</FONT>
<A
href=letters/show.phtml?aid=2005103019313399
target=_blank>迷山鬼话</A> </TD>
<TD>6744</TD>
<TD onmouseover=&quot;mOvr('intr3')&quot; onmouseout=&quot;mOut('intr3')&quot;>平生不肖生
<DIV id=intr3
style=&quot;VISIBILITY: hidden; POSITION: absolute&quot;>一曲豪歌济世穷,两鬓萧索禁烟中.平生寥落浮云意,落迹江南不肖生
</DIV></TD>


主要是里面的连接信息中有的含有<B>,有的连接中含有“,有的没有,这样的,还要和不含有的兼容,我不知道该怎么做,写两个表达式也可以,主要是不能重复,第一个表达式匹配出来的第二个表达式中不能再匹配。大家看该怎么写啊?分不够我可以再加。
 
这是我对TRegExpr控件(http://anso.virtualave.net/)写的一个正则表达式:
<A[/s]*href=(&quot;)?[^&quot;/s]*[^&quot;>]*(&quot;)?>(<[^>]*>)*[^<]*
这是提取的结果,可以在这里再处理一下就行了:
<A href=&quot;ashow.php?order=title&amp;sid=9&quot;><B>文章名
<A href=&quot;ashow.php?order=byte&amp;sid=9&quot;><B>Byte
<A href=&quot;ashow.php?order=author&amp;sid=9&quot;><B>作者
<A href=&quot;ashow.php?order=publish&amp;sid=9&quot;><B>发表时间
<A href=&quot;ashow.php?order=click&amp;sid=9&quot;><B>阅读数
<A href=&quot;ashow.php?order=reply&amp;sid=9&quot;><B>评论数
<A href=ashow.php?order=recent&amp;sid=9><B>最新评论</B></A></TD></TR>
<A href=letters/show.phtml?aid=2005103021082688 target=_blank>魍魉生(1)
<A href=letters/show.phtml?aid=2005103019313399 target=_blank>迷山鬼话
 
谢谢你了,还能再改进一下吗?我对正则表达式老是弄不明白,这个表达式并不通用,因为还有许多其他类型的连接需要提取,我也是用的TRegExpr控件,有没有办法可以直接提取出里面的连接地址和连接信息,就是可以用类似下面的程序直接获得:


regex.Expression:='<(a|A)(.*?) (href|HREF)=[''&quot;]?([^''&quot;]+)[''&quot;]?[^>]*>(.*?)<//(a|A)>';
with regex do
begin
if Exec(s) then
begin
repeat
sl.Add(regex.Match[0]);
t1:=regex.Match[4];
urllist.Add(t1);
t2:=regex.Match[5];
until not ExecNext;
上面的这段表达式可以取得部分连接和信息,但是对其他的连接提取错误。该怎麽修改一下呢?多谢了,帮帮忙吧
 
procedure TForm1.Button1Click(Sender: TObject);
const
IPExpr = '<A[/s]*href=&quot;?([^&quot;/s>]*)[^&quot;>]*&quot;?>(<[^>]*>)*([^<]*)';
var
r: TRegExpr;
istr: string;
begin
r := TRegExpr.Create;
r.ModifierI := true;
try
istr := memo1.Text;
r.Expression := IPExpr;
if r.Exec (istr) then
REPEAT
Memo2.Lines.Add('URL='+r.Match[1]);
Memo2.Lines.Add('Title='+r.Match[3]);
UNTIL not r.ExecNext;
finally
r.Free;
end;
end;
运行结果:
URL=ashow.php?order=title&amp;sid=9
Title=文章名
URL=ashow.php?order=byte&amp;sid=9
Title=Byte
URL=ashow.php?order=author&amp;sid=9
Title=作者
URL=ashow.php?order=publish&amp;sid=9
Title=发表时间
URL=ashow.php?order=click&amp;sid=9
Title=阅读数
URL=ashow.php?order=reply&amp;sid=9
Title=评论数
URL=ashow.php?order=recent&amp;sid=9
Title=最新评论
URL=letters/show.phtml?aid=2005103021082688
Title=魍魉生(1)
URL=letters/show.phtml?aid=2005103019313399
Title=迷山鬼话
 
虽然不太对但还是谢谢了
 
顶部