集册 Java实例教程 使用正则表达式匹配超链接

使用正则表达式匹配超链接

欢马劈雪     最近更新时间:2020-01-02 10:19:05

724
使用正则表达式匹配超链接

import java.util.Scanner;/*from N o w J a v a . c o m*/

import java.util.regex.Matcher;

import java.util.regex.Pattern;


public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        String line = scanner.nextLine();

        StringBuffer sb = new StringBuffer();

        while(!line.equals("END")){

            sb.append(line);

            line = scanner.nextLine();

        }

        String html = sb.toString();


        Pattern aPattern = Pattern.compile("<a([^>]+)>");

        Matcher aMatcher = aPattern.matcher(html);


        Pattern hrefPat = Pattern.compile("href\\s*=\\s*([\"']+)(.+?)\\1");/**来 自 nowjava**/

        while(aMatcher.find()){

            String attrString = aMatcher.group(
展开阅读全文