-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathHackerRank_TagExtractionString.java
More file actions
33 lines (27 loc) · 1.11 KB
/
HackerRank_TagExtractionString.java
File metadata and controls
33 lines (27 loc) · 1.11 KB
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
import java.util.*;
import java.util.regex.*;
public class HackerRank_TagExtractionString{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int testCases = Integer.parseInt(in.nextLine());
while (testCases > 0) {
String line = in.nextLine();
boolean flag = false;
Pattern p = Pattern.compile("<(.+)>([^<]+)</\\1>");
// <(.+)> --> For matching the start tag
// ([^<]+) --> For matching the internal text but excluding the "<" sign
// "excluding the "<" sign" --> This will take care of nested tag as it won't accept the outer tags and only extract the text from the internal tags.
// </\\1> --> For matching the end tag. //1 matches the first tag only.
Matcher m = p.matcher(line);
while (m.find()) {
System.out.println(m.group(2));
flag = true;
}
if (! flag) {
System.out.println("None");
}
testCases--;
}
in.close();
}
}