java 正则学习

前言

在网上找了许多关于正则解析 URL,结果不是很满意,所以自己学习正则:

java url

那么解析 url 的代码如下:

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


public class Regex {

    public static void main(String[] args) {
        String ee="http:\/\/([\w-]+\.)*[\w-]+\.[\w-]+(\/[\w-]+)*\/[\w-]+\.(html|css|ico)";
        String dd="http:\/\/([\w-]+\.)*\w+\.\w+(\/\w+)*\/\w+\.(html|css|ico)";
        String d="http:\/\/([\w-]+\.)*\w+\.\w+([\w-]+\.)*\/\w+\.html";
        Matcher m = Pattern.compile(ee)
                  .matcher("sss   http://ww-w.sohu.com/fx-y/index.html fff  <link rel="Stylesheet" type="text/css" href="http://static.blog.csdn.net/skin/default/css/style.html?v=1.1" />  <link id="RSSLink" title="RSS" type="application/rss+xml" rel="alternate" href="/allwefantasy/rss/list" /><link rel="shortcut icon" href="http://c.csdnimg.cn/public/favicon.ico" /><link type="text/css" rel="stylesheet" href="http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/default.css" />");
            while(m.find())
                  System.out.println(m.group());
            int i = 0;
            while(m.find(i)) {
                  System.out.print(m.group() + " ");
                  i++;
                }

    }

}

结果显示

原文地址:https://www.cnblogs.com/gzbit-zxx/p/7016171.html