正则表达式 练习:解析URL

来自姬鸿昌的知识库
Jihongchang讨论 | 贡献2022年11月25日 (五) 19:10的版本 →‎自己实现
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

https://www.bilibili.com/video/BV1Eq4y1E79W?p=26

对一个URL进行解析 "https://www.sohu.com:8080/abc/index.html"

  • 要求得到协议是什么? http
  • 域名是什么? www.sohu.com
  • 端口是什么? 8080
  • 文件名是什么? index.htm

自己实现

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Homework03 {

    public static void main(String[] args) {

        String content = "https://www.sohu.com:8080/abc/index.html";

        String regex = "^(http[s]?)://((\\w+\\.)+\\w+):(\\d+)(/\\w+)+/(\\w+\\.\\w+)$";

        Matcher matcher = Pattern.compile(regex).matcher(content);


        if (matcher.find()) {

            System.out.println(matcher.group(1));

            System.out.println(matcher.group(2));

            System.out.println(matcher.group(4));

            System.out.println(matcher.group(6));

        }

    }

}
https
www.sohu.com
8080
index.html



视频实现

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Homework03 {

    public static void main(String[] args) {

        String content = "http://www.sohu.com:8080/abc/index.html";

        String regStr = "^([a-zA-Z]+)://([a-zA-Z.]+):(\\d+)[\\w-/]*/([\\w.]+)$";

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        if (matcher.matches()) { //整体匹配,如果匹配成功,可以通过group(x),获取对应分组的内容

            System.out.println("整体匹配=" + matcher.group(0));

            System.out.println("协议:" + matcher.group(1));

            System.out.println("域名:" + matcher.group(2));

            System.out.println("端口:" + matcher.group(3));

            System.out.println("文件:" + matcher.group(4));

        } else {

            System.out.println("没有匹配成功");

        }

    }//end main

}
整体匹配=http://www.sohu.com:8080/abc/index.html
协议:http
域名:www.sohu.com
端口:8080
文件:index.html