-
Notifications
You must be signed in to change notification settings - Fork 4
/
Detect HTML links
48 lines (43 loc) · 1.46 KB
/
Detect HTML links
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.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) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
String string;
String regexOnlyTag = "[<][\\W]*+[a][\\W]*href=\"[\\W]*[^>]+";
String regexHTML = "[>][^>]*[<][\\W]*[/][\\W]*[a|b|h|H][\\w]{0,1}[\\W]*[>]";
String regexATag = "[\\[]";
Pattern tagPattern = Pattern.compile(regexOnlyTag);
Pattern htmlPattern = Pattern.compile(regexHTML);
Set<String> tagSet = new HashSet<String>();
for (int i = 0; i < n; i++)
{
string = in.nextLine();
Matcher tagMatcher = tagPattern.matcher(string);
Matcher htmlMatcher = htmlPattern.matcher(string);
while(tagMatcher.find())
{
String temp = string.substring(tagMatcher.start(), tagMatcher.end());
temp = temp.trim();
String[] temparr = temp.split("\"");
String tempNew = temparr[1].trim();
System.out.print(tempNew+",");
if(htmlMatcher.find())
{
String temp2 = string.substring(htmlMatcher.start(), htmlMatcher.end());
temp2 = temp2.trim();
String[] temparr2 = temp2.split("<");
String tempNew2 = temparr2[0].replace(">", "").trim();
System.out.println(tempNew2);
}
else
System.out.println();
}
}
}
}