Tag Content Extractor

  • + 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.