确定数组列表是否开始和结束并显示

时间:2015-10-03 09:05:23

标签: java arrays

我正在尝试过滤包含存储在(List<String> quotes = new ArrayList<>();)中的网址内容的数组列表,并显示<pre> </pre>个标记之间的所有内容的结果(全部引号放在这两个标签之间)。我已经找到了打印部分但是java中是否有任何方法允许您按照我指定的方式过滤数组列表?感谢

更多细节:

所以你有你的普通html文件,其中包含各种标签。假设我扫描页面并将所有文本存储在字符串数组中。我想只显示<pre></pre>标签之间的内容,而不是其他内容。希望这有帮助

以下是文本的存储方式:

List<String> cookies = new ArrayList<>();    
public void init() throws ServletException 
    {
        try 
        {
         URL url = new URL(" http://fortunes.cat-v.org/openbsd/");
             BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
         String line ;

           while((line = in.readLine()) != null)
           {
          cookies.add(line);
          //line = in.readLine();
           }
         in.close(); 
     }

    catch (java.net.MalformedURLException e) 
    {
      System.out.println("Malformed URL: " + e.getMessage());
    }
    catch (IOException e) 
    {
      System.out.println("I/O Error: " + e.getMessage());
    }

}

2 个答案:

答案 0 :(得分:0)

你可以找到String&#34; pre&#34;的索引。和&#34; / pre&#34;的索引和

之间所有元素的循环
int startIndex=quotes.IndexOf("<pre>");
int endIndex=c.IndexOf("</pre>");

for(int i=startIndex ; i<=endIndex ; i++){
  // do something here ... 
  // System.out.println(quotes.get(i));
}

答案 1 :(得分:0)

使用正则表达式,这是一个完整的工作示例

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;




public class Test {
    public static void main(String [] args){
        //This list is supposed filled with some values
        List<String> quotes = new ArrayList<String>();

        for(String quote:quotes){
            Pattern pattern = Pattern.compile(".*?<pre>(.*?)</pre>.*?");
            Matcher m = pattern.matcher(quote);
            while(m.find()){
                String result = m.group(1);
                System.out.println(result);
            }
        }


}

}