要求是对一个文本内容(一个list页面,包含有多条格式一样的内容),现要把这个文本中的多个相似的内容分别取出来,我编写了如下不完整的方法,用到是org.apache.oro.text.regex.*,开发工具是eclipse3.1。
private static String splitStore(String content, int num) {
try {
BufferedReader in;
Pattern pattern = new Perl5Compiler().compile("正规表达式");
in = new BufferedReader(new FileReader("content"));
String s;
String str[];
while ((s = in.readLine()) != null) {
PatternMatcher matcher = new Perl5Matcher();
if (matcher.contains(s, pattern)) {
MatchResult result = matcher.getMatch();
str[num] = result.group(1);
PatternMatcherInput input = new PatternMatcherInput(str[num]);
while (matcher.contains(input,pattern)){
result = matcher.getMatch();
str[++num] = result.group(++num);
}
}
} in.close();
} catch (Exception e) {
}
return null; }求帮忙完善或其他方法,谢谢!
private static String splitStore(String content, int num) {
try {
BufferedReader in;
Pattern pattern = new Perl5Compiler().compile("正规表达式");
in = new BufferedReader(new FileReader("content"));
String s;
String str[];
while ((s = in.readLine()) != null) {
PatternMatcher matcher = new Perl5Matcher();
if (matcher.contains(s, pattern)) {
MatchResult result = matcher.getMatch();
str[num] = result.group(1);
PatternMatcherInput input = new PatternMatcherInput(str[num]);
while (matcher.contains(input,pattern)){
result = matcher.getMatch();
str[++num] = result.group(++num);
}
}
} in.close();
} catch (Exception e) {
}
return null; }求帮忙完善或其他方法,谢谢!
List<String> result = new ArrayList<String>();BufferedReader in = new BufferedReader(new FileReader(content));
String tempStr=null;while((tempStr = in.readLine()) != null)
{
Matcher m = p.matcher(tempStr);
for(int i=0;i<m.groupCount();i++)
result.add(m.group(i));
}in.close();in = null;
还有我的方法是想根据splitStore(String content, int num)中的num取出页面中对应的内容;如splitStore(content, 1)能取到group(1)中的内容