Hackerrank Tag Content Extractor Solution
1 min read

Hackerrank Tag Content Extractor Solution

Hackerrank Tag Content Extractor Solution

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;
    }
}

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×