“正则表达式 验证复杂URL”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
第124行: 第124行:
  
 
=== 匹配域名后面的路径和参数 ===
 
=== 匹配域名后面的路径和参数 ===
'''<big>注意:当?出现在[]中,它将仅代表字符 ?,不再代表限定符</big>'''
+
'''<big>注意:当 ? 出现在[]中,它将仅代表字符 ?,不再代表限定符;当 . 出现在[]中,也将仅代表字符 .,不再匹配所有字符</big>'''<syntaxhighlight lang="java">
 +
import java.util.regex.Matcher;
 +
import java.util.regex.Pattern;
 +
 
 +
public class RegExp11 {
 +
 
 +
    public static void main(String[] args) {
 +
 
 +
        String content = "https://www.bilibili.com/vide0/BV1fh411y7R8?from=search&seid=1831060912083761326";
 +
 
 +
        /**
 +
        * 思路:
 +
        * 1、先确定 url 的开始部分 https:// | http://  ((http|https)://)
 +
        * 2、确定域名的部分 ([\w-]+\.)+[\w-]+
 +
        * 3、匹配域名后面的路径和参数  (\\/[\\w-?=&/%.]*)?
 +
        */
 +
        String regStr = "^((http|https)://)([\\w-]+\\.)+[\\w-]+(\\/[\\w-?=&/%.]*)?$";
 +
 
 +
        Pattern pattern = Pattern.compile(regStr);
 +
 
 +
        Matcher matcher = pattern.matcher(content);
 +
 
 +
        if (matcher.find()) {
 +
 
 +
            System.out.println("满足格式");
 +
 
 +
        } else {
 +
 
 +
            System.out.println("不满足格式");
 +
 
 +
        }
 +
 
 +
    }
 +
 
 +
}
 +
</syntaxhighlight><syntaxhighlight lang="console">
 +
满足格式
 +
</syntaxhighlight>

2022年11月19日 (六) 07:41的版本

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

先确定 URL 的开始部分

^((http|https)://)

示例:验证 https://

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

public class RegExp11 {

    public static void main(String[] args) {

        String content = "https://";

        /**
         * 思路:
         * 1、先确定 url 的开始部分 https:// | http://
         */
        String regStr = "^((http|https)://)$";

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        if (matcher.find()) {

            System.out.println("满足格式");

        } else {

            System.out.println("不满足格式");

        }

    }

}
满足格式


示例:验证 http://

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

public class RegExp11 {

    public static void main(String[] args) {

        String content = "http://";

        /**
         * 思路:
         * 1、先确定 url 的开始部分 https:// | http://
         */
        String regStr = "^((http|https)://)$";

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        if (matcher.find()) {

            System.out.println("满足格式");

        } else {

            System.out.println("不满足格式");

        }

    }

}
满足格式


确定域名的部分

([\w-]+\.)+[\w-]+

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

public class RegExp11 {

    public static void main(String[] args) {

        String content = "https://www.bilibili.com";

        /**
         * 思路:
         * 1、先确定 url 的开始部分 https:// | http://  ((http|https)://)
         * 2、确定域名的部分 ([\w-]+\.)+[\w-]+
         */
        String regStr = "^((http|https)://)([\\w-]+\\.)+[\\w-]+$";

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        if (matcher.find()) {

            System.out.println("满足格式");

        } else {

            System.out.println("不满足格式");

        }

    }

}
满足格式


匹配域名后面的路径和参数

注意:当 ? 出现在[]中,它将仅代表字符 ?,不再代表限定符;当 . 出现在[]中,也将仅代表字符 .,不再匹配所有字符

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

public class RegExp11 {

    public static void main(String[] args) {

        String content = "https://www.bilibili.com/vide0/BV1fh411y7R8?from=search&seid=1831060912083761326";

        /**
         * 思路:
         * 1、先确定 url 的开始部分 https:// | http://  ((http|https)://)
         * 2、确定域名的部分 ([\w-]+\.)+[\w-]+
         * 3、匹配域名后面的路径和参数  (\\/[\\w-?=&/%.]*)?
         */
        String regStr = "^((http|https)://)([\\w-]+\\.)+[\\w-]+(\\/[\\w-?=&/%.]*)?$";

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        if (matcher.find()) {

            System.out.println("满足格式");

        } else {

            System.out.println("不满足格式");

        }

    }

}
满足格式