获取html中styles内容

发布时间 2023-04-29 16:56:44作者: laremehpe
public class getStyle {

    public static void main(String[] args) throws FileNotFoundException {
        try {
            getStyle.handler();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedFlavorException e) {
            throw new RuntimeException(e);
        }
    }

    public static void handler() throws IOException, UnsupportedFlavorException {
        String str = get().replaceAll("\\n|\\s","");
        Matcher m = Pattern.compile("(?<=style=(\"|'))[a-zA-Z0-9;:\\.-]*",Pattern.MULTILINE).matcher(str);
        while (m.find()){
            System.out.println("\""+m.group()+"\",");
        }
    }

    static Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

    public static String get() throws UnsupportedFlavorException, IOException {
        Transferable content = clipboard.getContents(null);
        if (content.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String text = (String) content.getTransferData(DataFlavor.stringFlavor);
            return text;
        }
        System.out.println("error!");
        System.exit(0);
        return null;
    }

}