You've successfully subscribed to The Poor Coder | Hackerrank Solutions
Great! Next, complete checkout for full access to The Poor Coder | Hackerrank Solutions
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Hackerrank Tag Content Extractor Solution

Hackerrank Tag Content Extractor Solution

Beeze Aal
Beeze Aal

Solution in java8

Approach 1.

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){
      
       Pattern pattern = Pattern.compile("<([^>]+)>([^<]+)</\\1>");
       
      Scanner in = new Scanner(System.in);
      int testCases = Integer.parseInt(in.nextLine());
      while(testCases>0){
         String line = in.nextLine();
         Matcher m = pattern.matcher(line);
          int matches = 0;
          while(m.find()) {
              matches++;
              System.out.println(m.group(2));
          }
          if(matches == 0) {
              System.out.println("None");
          }
         
         testCases--;
      }
   }
}

Approach 2.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    private static String tagReg = "<(.+)>([^<]+)</\\1>";
    private static Pattern tagPattern = Pattern.compile(tagReg);     
    
    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();
         
            Matcher tagMatcher = tagPattern.matcher(line);
            if (tagMatcher.find()) {
                do {
                    System.out.println(tagMatcher.group(2));             
                } while (tagMatcher.find());
            } else {
                System.out.println("None");
            }
            testCases--;
       }
   }
}

Approach 3.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;


public class Solution
{
    public static void main(String[] args) throws IOException {
        final Pattern tagRE = Pattern.compile("<([^/>]+)>([^<>]+)</\\1>");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 64 * 1024);

        final int T = Integer.parseInt(br.readLine().trim(), 10);

        for (int t = 0; t < T; t++) {
            final String line = br.readLine().trim();

            final List<String> res = new ArrayList<>();
            Matcher match = tagRE.matcher(line);

            while (match.find()) {
                res.add(match.group(2));
            }

            if (res.size() == 0) {
                System.out.println("None");
            } else {
                System.out.println(res.stream().collect(Collectors.joining("\n")));
            }
        }

        br.close();
        br = null;
    }
}