Detect HTML Tags

  • + 1 comment

    Java 15

    import java.io.*;
    import java.util.*;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    public class Solution {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt(); scanner.nextLine();
            //one or more spaces before tagname. So \\s*
            Pattern pattern = Pattern.compile("<\\s*(\\w+)[^>]*>");
            TreeSet<String> tags = new TreeSet<String>();
            for(int i=0;i<n;i++)
            {   Matcher matcher = pattern.matcher(scanner.nextLine());
                while(matcher.find())   tags.add(matcher.group(1));
            }
            System.out.println(String.join(";",tags));
        }
    }