This repository was archived by the owner on Aug 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathTagContent.java
More file actions
39 lines (33 loc) · 1.32 KB
/
TagContent.java
File metadata and controls
39 lines (33 loc) · 1.32 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
34
35
36
37
38
39
/*
Given a string of text in a tag-based language, parse this text and retrieve the contents enclosed within sequences of well-organized tags meeting the following criterion:
The name of the start and end tags must be same. The HTML code <h1>Hello World</h2> is not valid, because the text starts with an h1 tag and ends with a non-matching h2 tag.
Tags can be nested, but content between nested tags is considered not valid. For example, in <h1><a>contents</a>invalid</h1>, contents is valid but invalid is not valid.
Tags can consist of any printable characters.
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class TagContent{
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();
String pattern= "(<)(.+)(>)([-\\w!@\\$%^&#\\*()_+|~=`\"\\{\\}\\[\\]:'\\, ]+)(</)(\\2)(>)";
Pattern r = Pattern.compile(pattern, Pattern.MULTILINE);
Matcher m = r.matcher(line);
if (m.find()) {
do {
System.out.println(m.group(4));
}
while (m.find());
}
else {
System.out.println("None");
}
testCases--;
}
}
}