我通过查找字符串索引是可以找到其位置的 int startId = str.indexOf("<table border=0 cellspacing=1 cellpadding=1 >");我现在的需求是需要拿出<table border=0 cellspacing=1 cellpadding=1 >开始 ,</table>结尾的表格中所有的内容。如下<table border=0 cellspacing=1 cellpadding=1 >... (我需要这里所有的内容,这里可能嵌套一些其他的标签<div><span>这类的,但是没有<table></table>之类的)</table>我使用这个正则好像匹配不到:
<table border=0 cellspacing=1 cellpadding=1 >(.*)</table>
请问各位高手,我应该 怎么获取<table border=0 cellspacing=1 cellpadding=1 >...</table>中的内容呢???
<table border=0 cellspacing=1 cellpadding=1 >(.*)</table>
请问各位高手,我应该 怎么获取<table border=0 cellspacing=1 cellpadding=1 >...</table>中的内容呢???
String pattern = "(<table border=0 cellspacing=1 cellpadding=1 >)(.*?)(</table>)";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(str);
if(m.find()){
System.out.println(m.group(2));
}
<div style="float:left; width:33px;margin-top:1px; padding-top:1px;">
<a href="http://www.zuobus.com/search_by_co.php?q=&qt=co&city=96" class="green1" target="_blank"><strong>< ></a>
</div>
<div style="float:left;"><a href="/hongkong arch.php?q=N118+%28%E4%B9%9D%E5%B7%B4%2F%E5%9F%8E%E5%B7%B4%29&schtype=0&qt=l&btnD=1" class="title1">Nx</a>
</div></table>
String str = "<table border=0 cellspacing=1 cellpadding=1 ><tr><td rowspan='2' bgcolor='#CFF1EA' valign='top' style='padding-left:3px;padding-right:3px;padding-bottom:5px;padding- top:3px;'><div style='width:250px;'><div style='float:left; width:33px;margin-top:1px; padding-top:1px;'><a href='http://www.zuobus.com/search_by_co.php?q=&qt=co&city=96' class='green1' target='_blank'><strong>< ></a></div><div style='float:left;'><a href='/hongkong arch.php?q=N118+%28%E4%B9%9D%E5%B7%B4%2F%E5%9F%8E%E5%B7%B4%29&schtype=0&qt=l&btnD=1' class='title1'>Nx</a></div></table>";
String pattern = "(<table border=0 cellspacing=1 cellpadding=1 >)(.*?)(</table>)";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(str);
if(m.find()){
System.out.println(m.group(2));
}
运行结果:
<tr><td rowspan='2' bgcolor='#CFF1EA' valign='top' style='padding-left:3px;padding-right:3px;padding-bottom:5px;padding- top:3px;'><div style='width:250px;'><div style='float:left; width:33px;margin-top:1px; padding-top:1px;'><a href='http://www.zuobus.com/search_by_co.php?q=&qt=co&city=96' class='green1' target='_blank'><strong>< ></a></div><div style='float:left;'><a href='/hongkong arch.php?q=N118+%28%E4%B9%9D%E5%B7%B4%2F%E5%9F%8E%E5%B7%B4%29&schtype=0&qt=l&btnD=1' class='title1'>Nx</a></div>