网址(URL)

2024-06-24 09:39:20 489
正则表达式匹配网址(URL)

正则表达式

/^(((ht|f)tps?):\/\/)?([^!@#$%^&*?.\s-]([^!@#$%^&*?.\s]{0,63}[^!@#$%^&*?.\s])?\.)+[a-z]{2,6}\/?/

解析

  • ^:匹配字符串的开始。
  • (((ht|f)tps?)://)?:匹配协议部分(可选)。
    • (ht|f)tps?:匹配 http, https, ftp, ftps
    • ://:匹配 ://
    • ?:表示前面的组是可选的(0 次或 1 次)。
  • (^!@#$%^&*?.\s-?.)+:匹配主机名部分。
    • [^!@#$%^&*?.\s-]:匹配主机名的第一个字符,不能包含特定的特殊字符(例如 !@#$%^&*?.)和空格。
    • ([^!@#$%^&?.\s]{0,63}[^!@#$%^&?.\s])?:匹配主机名中间部分(0 到 63 个字符),同样不能包含特定的特殊字符和空格。
    • .:匹配点 .
    • +:匹配前面的模式 1 次或多次。
  • [a-z]{2,6}:匹配顶级域名部分(长度在 2 到 6 个小写字母之间)。
  • /?:匹配可选的斜杠 /(0 次或 1 次)。
  • $:匹配字符串的结束。

示例

匹配成功的示例:

  • "http://example.com"
  • "https://example.co"
  • "ftp://example.net/"
  • "example.org"
  • "sub.domain.example.com"

匹配失败的示例:

  • "htp://example.com"(协议部分错误)
  • "http://example!.com"(主机名包含非法字符 !
  • "http://example..com"(主机名中有连续的点)
  • "http://example.commmmm"(顶级域名部分超过 6 个字符)
  • "http://example com"(主机名中有空格)

各种编程语言中的用法

Python

import re

pattern = r'^(((ht|f)tps?):\/\/)?([^!@#$%^&*?.\s-]([^!@#$%^&*?.\s]{0,63}[^!@#$%^&*?.\s])?\.)+[a-z]{2,6}\/?'
test_strings = ["http://example.com", "https://example.co", "ftp://example.net/", "example.org", "sub.domain.example.com", "htp://example.com", "http://example!.com", "http://example..com", "http://example.commmmm", "http://example com"]

for test_string in test_strings:
    if re.match(pattern, test_string):
        print(f"'{test_string}': 匹配成功")
    else:
        print(f"'{test_string}': 匹配失败")

JavaScript

const pattern = /^(((ht|f)tps?):\/\/)?([^!@#$%^&*?.\s-]([^!@#$%^&*?.\s]{0,63}[^!@#$%^&*?.\s])?\.)+[a-z]{2,6}\/?/;
const testStrings = ["http://example.com", "https://example.co", "ftp://example.net/", "example.org", "sub.domain.example.com", "htp://example.com", "http://example!.com", "http://example..com", "http://example.commmmm", "http://example com"];

testStrings.forEach(testString => {
    if (pattern.test(testString)) {
        console.log(`'${testString}': 匹配成功`);
    } else {
        console.log(`'${testString}': 匹配失败`);
    }
});

Java

import java.util.regex.*;

public class RegexTest {
    public static void main(String[] args) {
        String pattern = "^(((ht|f)tps?):\\/\\/)?([^!@#$%^&*?.\\s-]([^!@#$%^&*?.\\s]{0,63}[^!@#$%^&*?.\\s])?\\.)+[a-z]{2,6}\\/?";
        String[] testStrings = {"http://example.com", "https://example.co", "ftp://example.net/", "example.org", "sub.domain.example.com", "htp://example.com", "http://example!.com", "http://example..com", "http://example.commmmm", "http://example com"};
        
        for (String testString : testStrings) {
            if (Pattern.matches(pattern, testString)) {
                System.out.println("'" + testString + "': 匹配成功");
            } else {
                System.out.println("'" + testString + "': 匹配失败");
            }
        }
    }
}

C#

using System;
using System.Text.RegularExpressions;

class Program {
    static void Main() {
        string pattern = @"^(((ht|f)tps?):\/\/)?([^!@#$%^&*?.\s-]([^!@#$%^&*?.\s]{0,63}[^!@#$%^&*?.\s])?\.)+[a-z]{2,6}\/?";
        string[] testStrings = {"http://example.com", "https://example.co", "ftp://example.net/", "example.org", "sub.domain.example.com", "htp://example.com", "http://example!.com", "http://example..com", "http://example.commmmm", "http://example com"};
        
        foreach (string testString in testStrings) {
            if (Regex.IsMatch(testString, pattern)) {
                Console.WriteLine($"'{testString}': 匹配成功");
            } else {
                Console.WriteLine($"'{testString}': 匹配失败");
            }
        }
    }
}

PHP

<?php
$pattern = '/^(((ht|f)tps?):\/\/)?([^!@#$%^&*?.\s-]([^!@#$%^&*?.\s]{0,63}[^!@#$%^&*?.\s])?\.)+[a-z]{2,6}\/?/';
$testStrings = ["http://example.com", "https://example.co", "ftp://example.net/", "example.org", "sub.domain.example.com", "htp://example.com", "http://example!.com", "http://example..com", "http://example.commmmm", "http://example com"];

foreach ($testStrings as $testString) {
    if (preg_match($pattern, $testString)) {
        echo "'$testString': 匹配成功\n";
    } else {
        echo "'$testString': 匹配失败\n";
    }
}
?>

Ruby

pattern = /^(((ht|f)tps?):\/\/)?([^!@#$%^&*?.\s-]([^!@#$%^&*?.\s]{0,63}[^!@#$%^&*?.\s])?\.)+[a-z]{2,6}\/?/
test_strings = ["http://example.com", "https://example.co", "ftp://example.net/", "example.org", "sub.domain.example.com", "htp://example.com", "http://example!.com", "http://example..com", "http://example.commmmm", "http://example com"]

test_strings.each do |test_string|
  if pattern.match?(test_string)
    puts "'#{test_string}': 匹配成功"
  else
    puts "'#{test_string}': 匹配失败"
  end
end

这些关键词可以帮助用户