import java.io.*;
import java.util.regex.*;
import java.net.*;
import java.util.*;
class T
{
public static void main(String ddd[])throws Exception
{
URL url=new URL("http://www.baidu.com/s?lm=0&si=&rn=10&ie=gb2312&ct=0&wd=java&pn=100&cl=3");
InputStream in=(InputStream)url.getContent();
StringBuffer s=new StringBuffer(16000);
byte[] b=new byte[1024];
while(in.read(b)!=-1)
{
s.append(new String(b,"gb2312"));
}
Pattern p=Pattern.compile("<a");
Matcher m=p.matcher(s);
System.out.println(m.matches());
}
}百度上肯定有<a> 但是怎么都返回false
import java.util.regex.*;
import java.net.*;
import java.util.*;
class T
{
public static void main(String ddd[])throws Exception
{
URL url=new URL("http://www.baidu.com/s?lm=0&si=&rn=10&ie=gb2312&ct=0&wd=java&pn=100&cl=3");
InputStream in=(InputStream)url.getContent();
StringBuffer s=new StringBuffer(16000);
byte[] b=new byte[1024];
while(in.read(b)!=-1)
{
s.append(new String(b,"gb2312"));
}
Pattern p=Pattern.compile("<a");
Matcher m=p.matcher(s);
System.out.println(m.matches());
}
}百度上肯定有<a> 但是怎么都返回false
匹配子序列用m.find()
s.indexOf("<a")来达到你要判断<a的效果
匹配子序列用m.find()
这个说得对。
最后一句改为:System.out.println(m.find());就好了