forked from animesh0353/Hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Detect the Domain Name
48 lines (43 loc) · 1.33 KB
/
Detect the Domain Name
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
Set<String> set = new HashSet<String>();
ArrayList<String> emailList = new ArrayList<String>();
String regex = "http[s]?[:][/][/][\\w|-]+([\\.][\\w|-]+)+"; //"[\\w|_]+[@][\\w]+([\\.][\\w]+)+";
// String regex = "http[s]?[:][/][/][\\w]+[\\.][^/|^ ]+";
Pattern pattern = Pattern.compile(regex);
String string;
for (int i = 0; i < n; i++)
{
string = in.nextLine();
Matcher matcher = pattern.matcher(string);
while(matcher.find())
{
String tempEmail = string.substring(matcher.start(), matcher.end());
String[] domain = tempEmail.trim().split("//");
String temp = domain[1].trim().replace("www.", "");
temp = temp.replace("_", "");
set.add(temp.trim());
}
}
emailList.addAll(set);
Collections.sort(emailList);
StringBuffer strbuff = new StringBuffer();
for(String email : emailList)
strbuff.append(email+";");
if(strbuff.length() > 1)
{
strbuff.deleteCharAt(strbuff.length() - 1);
System.out.println(strbuff.toString());
}
}
}