Skip to content

Commit 9d08c53

Browse files
committed
Appplications based Regex Solved
1 parent 657a987 commit 9d08c53

10 files changed

+314
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.util.*;
2+
import java.util.regex.*;
3+
4+
public class Solution {
5+
private final static Matcher matcher = Pattern.compile(
6+
//Http
7+
"(?:https?://)"
8+
9+
//Domain
10+
+"(?<domain>[-a-z0-9_~]{1,63}(?:\\.[-a-z0-9_~]{1,63}){1,126})"
11+
12+
//Port
13+
+"(?::[0-9]{1,5})?"
14+
15+
//Path
16+
+"(?:/[^\\s?]*)?"
17+
18+
//Parameters
19+
+"(?:\\?[-a-z0-9_~]+=[-a-z0-9_~]*(?:&[-a-z0-9_~]+=[-a-z0-9_~]*)*)?",
20+
21+
Pattern.CASE_INSENSITIVE
22+
).matcher("");
23+
24+
public static void main(String[] args) {
25+
Scanner in = new Scanner(System.in);
26+
Set<String> domains = new TreeSet<String>();
27+
28+
//Find domains
29+
int N = Integer.parseInt(in.nextLine());
30+
while (N-- > 0) {
31+
String htmlFrag = in.nextLine();
32+
matcher.reset(htmlFrag);
33+
while (matcher.find()) {
34+
String domain = matcher.group("domain").toLowerCase();
35+
if (domain.length() < 254) {
36+
if (domain.startsWith("www.") || domain.startsWith("ww2.")) {
37+
domain = domain.substring(4);
38+
}
39+
domains.add(domain);
40+
}
41+
}
42+
}
43+
44+
//Print domains
45+
N = domains.size();
46+
for (String domain : domains) {
47+
System.out.print(domain + (--N > 0 ? ";" : ""));
48+
}
49+
}
50+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.util.regex.*;
4+
5+
public class Solution {
6+
7+
public static void main(String[] args) throws IOException{
8+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
9+
Scanner in = new Scanner(System.in);
10+
Matcher matcher = Pattern.compile(
11+
"(?<local>"
12+
13+
+ "[-a-zA-Z0-9!#$%&'*+/=?^_`{|}~.]+(?:\\.\\\"(?:[- a-zA-Z0-9!#$%&'*+/=?^_`{|}~.(),:;<>@\\[\\]]|\\\\\"|\\\\)+\\\"\\.[- a-zA-Z0-9!#$%&'*+/=?^_`{|}~.]+)?"
14+
15+
+ "|"
16+
17+
+ "\\\"(?:[- a-zA-Z0-9!#$%&'*+/=?^_`{|}~.(),:;<>@\\[\\]]|\\\\\"|\\\\)+\\\""
18+
19+
+ ")@(?<domain>"
20+
21+
//Hostname
22+
+ "[a-zA-Z0-9](?:[-a-zA-Z0-9]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[-a-zA-Z0-9]{0,61}[a-zA-Z0-9])?)*"
23+
24+
+ "|"
25+
26+
//IPv4
27+
+ "\\[(?:2(?:5[0-5]?|[0-4][0-9]?)?|[0-1][0-9]{0,2})(?:\\.(?:2(?:5[0-5]?|[0-4][0-9]?)?|[0-1][0-9]{0,2})){3}\\]"
28+
29+
+ "|"
30+
31+
//IPv6
32+
+ "\\[IPv6(?::[0-9a-f]{1,4}){0,8}(?::(?::[0-9a-f]{1,4}){0,8})?\\]"
33+
34+
+ ")"
35+
,Pattern.CASE_INSENSITIVE
36+
).matcher("");
37+
38+
Set<String> emails = new TreeSet<String>();
39+
40+
//For each line in the input
41+
for (int N = Integer.parseInt(in.nextLine()); N > 0; --N) {
42+
String line = in.nextLine();
43+
//Run through the regex
44+
matcher.reset(line);
45+
//And for each email address matched
46+
while (matcher.find()) {
47+
String domain = matcher.group("domain");
48+
if (domain.length() < 256) {
49+
//Store in the emails list
50+
emails.add(matcher.group());
51+
}
52+
}
53+
}
54+
55+
//Print emails
56+
int i = emails.size();
57+
for (String email : emails) {
58+
System.out.print(email + (--i > 0 ? ";" : ""));
59+
}
60+
in.close();
61+
}
62+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.*;
2+
import java.util.regex.*;
3+
4+
public class Solution {
5+
public static void main(String[] args) {
6+
Scanner in = new Scanner(System.in);
7+
Matcher matcher = Pattern.compile(
8+
"<\\s*a\\s+href\\s*=\\s*\\\""
9+
+ "(?<link>[^\\\\\"]*)"
10+
+ "\\\"[^>]*>(?:<[^>]*>)*"
11+
+ "(?<name>.*?)"
12+
+ "(?:<\\s*/[^>]*>)*<\\s*/\\s*a\\s*>",
13+
Pattern.CASE_INSENSITIVE
14+
).matcher("");
15+
16+
for (int N = Integer.parseInt(in.nextLine()); N > 0; N--) {
17+
String html = in.nextLine();
18+
matcher.reset(html);
19+
while (matcher.find()) {
20+
String link = matcher.group("link").trim();
21+
String name = matcher.group("name").trim();
22+
System.out.println(link+","+name);
23+
}
24+
}
25+
}
26+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.util.regex.*;
4+
5+
public class Solution {
6+
7+
public static void main(String[] args) throws IOException{
8+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
9+
Set<String> set = new TreeSet<String>();
10+
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
11+
Matcher m = Pattern.compile(
12+
13+
//Opening bracket and optional spaces
14+
"<\\s*"
15+
//Tag name
16+
+ "([a-z0-9]+)"
17+
18+
//Optional 1+ spaces and tag attributes
19+
+"(?:\\s(?:\\s*[a-z]+\\s*=\\s*\\\"[^\\\\\"]*\\\")*)?"
20+
21+
//Self-close tag or normal tag
22+
+"\\s*(?:\\/>|>)",
23+
24+
//Input is valid html so assume
25+
//existence closing tags and correct nesting
26+
27+
Pattern.CASE_INSENSITIVE
28+
).matcher("");
29+
30+
//Find all the tags in input
31+
int N = Integer.parseInt(reader.readLine());
32+
while (N-- > 0) {
33+
String line = reader.readLine();
34+
m.reset(line);
35+
while (m.find()) {
36+
set.add(m.group(1));
37+
}
38+
}
39+
40+
//Print out tags
41+
int count = 0;
42+
int size = set.size();
43+
for (String word : set) {
44+
System.out.print((++count < size) ? word + ";" : word);
45+
}
46+
}
47+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.io.*;
2+
import java.util.regex.*;
3+
public class Solution {
4+
public static void main(String[] args) throws IOException {
5+
StringBuffer sb = new StringBuffer();
6+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
Matcher matcher = Pattern.compile(
8+
"^\\("
9+
//Latitude
10+
+ "[-+]?(?:90(?:\\.0+)?|[1-8]?[0-9](?:\\.[0-9]+)?)"
11+
+ ", "
12+
//Longitude
13+
+ "[-+]?(?:180(?:\\.0+)?|(?:(?:[0-9]?[0-9]|1[0-7][0-9])(?:\\.[0-9]+)?))"
14+
+ "\\)$"
15+
).matcher("");
16+
for(byte N = Byte.parseByte(br.readLine()); N > 0; --N){
17+
matcher.reset(br.readLine());
18+
sb.append(matcher.matches() ? "Valid\n" : "Invalid\n");
19+
}
20+
System.out.print(sb);
21+
}
22+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.util.regex.*;
4+
public class Solution {
5+
6+
public static void main(String[] args) throws IOException{
7+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
8+
Map<String, Integer> map = new HashMap<String, Integer>();
9+
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
10+
Matcher m = Pattern.compile(
11+
"(?<![_0-9a-z])[_0-9a-z]+(?![_0-9a-z])",
12+
Pattern.CASE_INSENSITIVE
13+
).matcher("");
14+
int N = Integer.parseInt(reader.readLine());
15+
while (N-- > 0) {
16+
String line = reader.readLine();
17+
m.reset(line);
18+
while (m.find()) {
19+
String word = m.group();
20+
Integer count = map.get(word);
21+
map.put(word, (count == null) ? 1 : count+1);
22+
}
23+
}
24+
int T = Integer.parseInt(reader.readLine());
25+
while (T-- > 0) {
26+
String word = reader.readLine();
27+
Integer count = map.get(word);
28+
System.out.println((count == null) ? 0 : count);
29+
}
30+
reader.close();
31+
}
32+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.util.regex.*;
4+
5+
public class Solution {
6+
7+
public static void main(String[] args) throws IOException {
8+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
9+
final String KEY = "hackerrank";
10+
Scanner in = new Scanner(System.in);
11+
Matcher matcher = Pattern.compile(KEY, Pattern.CASE_INSENSITIVE).matcher("");
12+
//Count the tweets with KEY in it
13+
int count = 0;
14+
for(int N = Integer.parseInt(in.nextLine()); N > 0; --N) {
15+
String tweet = in.nextLine();
16+
matcher.reset(tweet);
17+
count += matcher.find() ? 1 : 0;
18+
}
19+
//Output count
20+
System.out.println(count);
21+
}
22+
}

Hackerrank/Regex/Applications/IDEComments.java

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.util.regex.*;
4+
5+
public class Solution {
6+
7+
public static void main(String[] args) throws IOException{
8+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
9+
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
10+
Matcher IPv4Matcher = Pattern.compile(
11+
"(?:2(?:5[0-5]?|[0-4][0-9]?|[0-9])?|[0-1][0-9]{0,2}|[3-9][0-9]?)(?:\\.(?:2(?:5[0-5]?|[0-4][0-9]?|[0-9])?|[0-1][0-9]{0,2}|[3-9][0-9]?)){3}"
12+
).matcher("");
13+
Matcher IPv6Matcher = Pattern.compile(
14+
"[0-9a-f]{1,4}(?::[0-9a-f]{1,4}){7}",
15+
Pattern.CASE_INSENSITIVE
16+
).matcher("");
17+
int N = Integer.parseInt(reader.readLine());
18+
while (N-- > 0) {
19+
String address = reader.readLine().trim();
20+
IPv4Matcher.reset(address);
21+
if (IPv4Matcher.matches()) {
22+
System.out.println("IPv4");
23+
continue;
24+
}
25+
IPv6Matcher.reset(address);
26+
if (IPv6Matcher.matches()) {
27+
System.out.println("IPv6");
28+
continue;
29+
}
30+
System.out.println("Neither");
31+
}
32+
reader.close();
33+
}
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.util.regex.*;
4+
public class Solution {
5+
6+
public static void main(String[] args) throws IOException{
7+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
8+
StringBuffer sb = new StringBuffer();
9+
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
10+
Matcher m = Pattern.compile("^[A-Z]{5}[0-9]{4}[A-Z]$").matcher("");
11+
int N = Integer.parseInt(reader.readLine());
12+
while (N-- > 0) {
13+
String line = reader.readLine();
14+
String output = m.reset(line).matches() ? "YES\n" : "NO\n";
15+
sb.append(output);
16+
}
17+
System.out.print(sb);
18+
}
19+
}

0 commit comments

Comments
 (0)