我正在尝试使用
Java解析带有正则表达式的链接.
但我认为它变得太慢了.例如,要从以下位置提取所有链接:
……花了34642毫秒(34秒!!!)
这是正则表达式:
private final String regexp = "|>)";
模式的标志:
private static final int flags = Pattern.CASE_INSENSITIVE | Pattern.DOTALL |Pattern.MULTILINE | Pattern.UNICODE_CASE | Pattern.CANON_EQ;
代码可能是这样的:
private void processURL(URL url){
URLConnection connection;
Pattern pattern = Pattern.compile(regexp, flags);
try {
connection = url.openConnection();
InputStream in = connection.getInputStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
String html = new String();
String line = bf.readLine();
while(line!=null){
html += line;
line = bf.readLine();
}
bf.close();
Matcher matcher = pattern.matcher(html);
while (matcher.find()) {
System.out.println(matcher.group(2));
}
} catch (Exception e){
}
}
你能给我一个提示吗?
额外数据:
为1Mbit
Core 2 Duo
1Gb RAM
单螺纹