如果有问题,请与我联系。 参考资料 java.util.regex的帮助文档
import java.io.*;
import java.net.*;
import java.util.regex.*;
/**
This program displays all URLs in a web page
by jgyang 2005-11-18
*/
public class HerfMatch
{
public static void main(String[] args)
{
try
{
String urlString = "
http://www.tom.com";
InputStreamReader in = new InputStreamReader(
new URL(urlString).openStream());
StringBuffer input = new StringBuffer();
int ch;
while ((ch = in.read()) != -1) input.append((char)ch);
String patternString
= "<a\\s+href\\s*=\\s*(\"[^\"]*\"|[^\\s>]|\"[^\"]*\"
\\s*+[^>\"]*\\s*=\\s*(\"[^\"]*\"|[^\\s>]))\\s*>";
Pattern pattern = Pattern.compile(patternString,
Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
int i = 0;
while (matcher.find())
{
int start = matcher.start();
int end = matcher.end();
String match = input.substring(start, end);
System.out.println(++i + " : " + match);
}
}
catch (IOException exception)
{
exception.printStackTrace();
}
catch (PatternSyntaxException exception)
{
exception.printStackTrace();
}
}
}