Advertisement
Guest User

Untitled

a guest
May 24th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. //Mateusz Pawelec I4B3S1//
  2.     import java.io.BufferedReader;
  3.     import java.io.FileInputStream;
  4.     import java.io.FileNotFoundException;
  5.     import java.io.IOException;
  6.     import java.io.InputStreamReader;
  7.     import java.io.UnsupportedEncodingException;
  8.     import java.util.LinkedList;
  9.     import java.util.List;
  10. import java.util.Scanner;
  11. import java.util.regex.Matcher;
  12.     import java.util.regex.Pattern;
  13.  
  14.  
  15. public class regexZad3 {
  16. //               wp.pl
  17.  
  18.         static String FILENAME = "wp.htm";
  19.        
  20.         public static void main(String[] args) {
  21.             Scanner keyboard = new Scanner(System.in);
  22.            
  23.             String message = "";
  24.             List<String> resultList = new LinkedList<String>();
  25.             BufferedReader in = null;
  26.             try {
  27.                 in = new BufferedReader(new InputStreamReader(new FileInputStream(FILENAME), "UTF-8"));
  28.             } catch (UnsupportedEncodingException | FileNotFoundException e1) {
  29.                 e1.printStackTrace();
  30.             }
  31.             System.out.println("Podaj ciag znakow ktore chcesz znalezc");
  32.             String input = keyboard.next();
  33.             String fileLine;
  34.             String pattern1 = ">(.*?"+input+".*?)<";
  35.             Pattern p1 = Pattern.compile(pattern1, Pattern.CASE_INSENSITIVE);
  36.            
  37.             try {
  38.                 while ((fileLine = in.readLine()) != null) {
  39.                     fileLine = fileLine + "<";
  40.                     Matcher m1 = p1.matcher(fileLine);
  41.                     if(m1.find()){
  42.                         String tmp = m1.group(1);
  43.                         int index = tmp.lastIndexOf(">");
  44.                         tmp = tmp.substring(index + 1);
  45.                         tmp = tmp.trim();
  46.                         if(tmp.indexOf("/") == -1)resultList.add(tmp);
  47.                     }
  48.                 }
  49.             } catch (IOException e) {
  50.                 e.printStackTrace();
  51.             }
  52.            
  53.             for(String str : resultList) {
  54.                 message += str;
  55.                 message += "\n";
  56.             }
  57.             System.out.println(message);
  58.            
  59.             try {
  60.                 in.close();
  61.             } catch (IOException e) {
  62.                 e.printStackTrace();
  63.             }
  64.         }
  65.  
  66.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement