We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This code run for intelliJ IDEA and output will be run successfully. But not for HackerRank Solution. output is generate None.
public static void main(String[] args) {
// Input: Number of lines followed by the lines of text
Scanner scanner = new Scanner(System.in);
int n;
try {
n = Integer.parseInt(scanner.nextLine().trim()); // Read the number of lines
} catch (NumberFormatException e) {
System.out.println("Invalid input for number of lines. Please provide a valid integer.");
return;
}
String[] lines = new String[n];
for (int i = 0; i < n; i++) {
lines[i] = scanner.nextLine(); // Read each line
}
scanner.close();
// Process each line and extract valid content
for (String line : lines) {
if (line == null || line.isEmpty()) {
System.out.println("None");
continue;
}
extractValidContent(line);
}
}
public static void extractValidContent(String line) {
// Regex to match valid tags and their contents
while (matcher.find()) {
String content = matcher.group(2);
// Check if content is not nested
if(content.contains("<")){
System.out.println(content);
found = true;
}
}
if (!found) {
System.out.println("None");
}
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Tag Content Extractor
You are viewing a single comment's thread. Return to all comments →
This code run for intelliJ IDEA and output will be run successfully. But not for HackerRank Solution. output is generate None.
public static void main(String[] args) { // Input: Number of lines followed by the lines of text Scanner scanner = new Scanner(System.in); int n;
// String regex = "<([a-zA-Z0-9\s]+)>(.*?)"; String regex = "<(.+)>([^<]+)"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(line); boolean found = false;
}