YavorGrancharov

URL_Parser

Jan 6th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class URL_Parser {
  6.     public static void main(String[] args) {
  7.         Scanner console = new Scanner(System.in);
  8.  
  9.         String line = console.nextLine();
  10.  
  11.         Pattern pattern = Pattern.compile("\\b([a-z]+)(?:\\:\\/\\/)([a-zA-Z]+\\.[a-zA-Z]+\\.[a-zA-Z]+)(?:\\/)(.+)\\b");
  12.         Matcher matcher = pattern.matcher(line);
  13.  
  14.         StringBuilder protocol = new StringBuilder();
  15.         StringBuilder server = new StringBuilder();
  16.         StringBuilder resource = new StringBuilder();
  17.  
  18.         while (matcher.find()) {
  19.             for (int i = 0; i < matcher.groupCount(); i++) {
  20.                 protocol.append(matcher.group(i));
  21.                 server.append(matcher.group(i + 1));
  22.                 resource.append(matcher.group(i + 2));
  23.  
  24.             }
  25.         }
  26.  
  27.         System.out.printf("[protocol] = \"%s\"\n",protocol.toString());
  28.         System.out.printf("[server] = \"%s\"\n",server.toString());
  29.         System.out.printf("[resource] = \"%s\"\n",resource.toString());
  30.     }
  31. }
Add Comment
Please, Sign In to add comment