Tag Content Extractor

  • + 0 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;

        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
    

    // String regex = "<([a-zA-Z0-9\s]+)>(.*?)"; String regex = "<(.+)>([^<]+)"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(line); boolean found = false;

        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");
        }
    }
    

    }