Detect HTML links

  • + 0 comments

    java solution

    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 = Integer.parseInt(scanner.nextLine());
            Pattern pattern = Pattern.compile("<a href=\"(\\S+)\"[^>]*>([^<]*)(</a>){0,1}(<(\\w+)>){0,1}([^<>]+){0,1}(</\\5>){0,1}(</a>){0,1}");
            for (int i = 0; i < n; i++) {
                Matcher m = pattern.matcher(scanner.nextLine());
                while (m.find()) System.out.println(m.group(1) + "," + (((m.group(6) == null) || (m.group(2) != "")) ? m.group(2).trim() : m.group(6)));
            }
        }
    }