Tag Content Extractor

Sort by

recency

|

218 Discussions

|

  • + 0 comments
    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution{
    	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(); 
                
                Pattern pattern = Pattern.compile("<([^<>]+)>([^<>]+)<\\/\\1>"); 
                Matcher matcher = pattern.matcher(line); 
                Stack<String> stack = new Stack<>();  
                 
                List<String> strsFound = new LinkedList<>();
                while (matcher.find()) {   
                    
                     if (matcher.group(2).length() > 0) strsFound.add(matcher.group(2)); 
                }
    			if (strsFound.size() == 0) {
                    System.out.println("None"); 
                }
                else {
                    for (String str: strsFound) {
                        System.out.println(str); 
                    }
                }
    			testCases--;
    		}
    	}
    }
    
  • + 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");
        }
    }
    

    }

  • + 0 comments

    For any reason the algorithm can't match this line

    start tag --> bbgnEsMIM%9L=E v)f6GjSkSK6W5HWdel)VbBvZRG)#&b=+6k(O9=&C

    value --> rulVew89#uyWF}4`T"xULOZ%1"5Cu)&x7qD0

    end tag --> /bbgnEsMIM%9L=E v)f6GjSkSK6W5HWdel)VbBvZRG)#&b=+6k(O9=&C

    I put it in Intellij and it works well but in Hackerrank page doesn't work, I found out this one in the sample test 3

  • + 0 comments
    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class TagExtractor {
        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();
    			
              	//Write your code here
            String tagRegex = "<(.+)>(\\w|\\d|\\s)+</\\1>";
            Pattern tagPattern = Pattern.compile(tagRegex);
            Matcher tagMatcher = tagPattern.matcher(line);
    
            String result = "None";
            if (tagMatcher.find()) {
              String tagName = tagMatcher.group(1);
              result = tagMatcher.group(0).replaceAll("<" + tagName + ">", "");
              result = result.substring(0, result.indexOf("<"));
            }
            System.out.println(result);
    
            testCases--;
    		}
      }
    }
    

    For some reason I got errors.

  • + 1 comment

    import java.util.; import java.util.regex.;

    public class Solution {

    public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
        int testCases = Integer.parseInt(scan.nextLine());
    
        while (testCases-- > 0) {
            String line = scan.nextLine();
    
            Pattern r = Pattern.compile("<(.+)>([^<]+)</\\1>");
            Matcher m = r.matcher(line);
    
            if (m.find()){
           do {
               System.out.println(m.group(2));
               } while (m.find());
            } else{
                System.out.println("None");
                }
        }
    }
    

    }