Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class URL_Parser {
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- String line = console.nextLine();
- Pattern pattern = Pattern.compile("\\b([a-z]+)(?:\\:\\/\\/)([a-zA-Z]+\\.[a-zA-Z]+\\.[a-zA-Z]+)(?:\\/)(.+)\\b");
- Matcher matcher = pattern.matcher(line);
- StringBuilder protocol = new StringBuilder();
- StringBuilder server = new StringBuilder();
- StringBuilder resource = new StringBuilder();
- while (matcher.find()) {
- for (int i = 0; i < matcher.groupCount(); i++) {
- protocol.append(matcher.group(i));
- server.append(matcher.group(i + 1));
- resource.append(matcher.group(i + 2));
- }
- }
- System.out.printf("[protocol] = \"%s\"\n",protocol.toString());
- System.out.printf("[server] = \"%s\"\n",server.toString());
- System.out.printf("[resource] = \"%s\"\n",resource.toString());
- }
- }
Add Comment
Please, Sign In to add comment