Tag Content Extractor

  • + 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());
            
            String tagContentRegex = "<([\\s\\S_ ]+)>([^<>]+)</\\1>";
            Pattern tagPattern = Pattern.compile(tagContentRegex);
            
            while (testCases>0) {
                String line = in.nextLine();
                boolean found = false;
                Matcher matcher = tagPattern.matcher(line);
                
                while (matcher.find()) {
                    String content = matcher.group(2);
                    System.out.println(content);
                    found = true;
                }
                if (!found) {
                    System.out.println("None");
                }
                testCases--;
            }
            in.close();
        }
    }