我有一些代码,它接收一个URL,读取文件并搜索与给定正则表达式匹配的字符串,并将任何匹配项添加到一个arrayList中,直到达到文件结尾。如何修改我的代码,以便在读取文件时,可以在同一次遍历中检查其他匹配其他正则表达式的字符串,而不必多次读取文件以检查每个不同的正则表达式?
//Pattern currently being checked for
Pattern name = Pattern.compile("<a id=.dg__ct(.+?)_hpl1.>(.+?)</a>");
//Pattern I want to check for as well, currently not implemented
Pattern date = Pattern.compile("[0-9]{2}/[0-9]{2}/[0-9]{4}");
Matcher m;
InputStream inputStream = null;
arrayList = new ArrayList<String>();
try {
URL url = new URL(
"URL to be read");
inputStream = (InputStream) url.getContent();
} catch (Exception e) {
e.printStackTrace();
} finally {
InputStreamReader isr = new InputStreamReader(inputStream);
BufferedReader buf = new BufferedReader(isr);
String str = null;
String s = null;
try {
while ((str = buf.readLine()) != null) {
m = name.matcher(str);
while(m.find()){
s = m.group();
arrayList.add(s);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}