Tag Content Extractor

Sort by

recency

|

216 Discussions

|

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

    }

  • + 0 comments
    Pattern p = Pattern.compile("<(.+)>([^<]+)</\\1>");
    Matcher m  = p.matcher(line);
    if (m.find()){
        do {System.out.println(m.group(2));}
         while (m.find());
                }
    else{System.out.println("None");}
    
  • + 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 && in.hasNextLine()){
            String line = in.nextLine();
            String[] lines = line.split("\n");
            for (String string : lines) {
                String regex = "<(.+)>([^<>]+)</\\1>";
                Pattern pattern = Pattern.compile(regex);
                Matcher matcher = pattern.matcher(string); 
                while (matcher.find()) {
                    String match = matcher.group(2);
                    System.out.println(match);             
                }
    
                matcher.reset();
    
                if (matcher.find() == false) {
                    System.out.println("None");
                }
            }
            testCases--;
        }
        in.close();
    }
    

    }**