迅雷链接

2024-06-24 09:41:46 550
匹配迅雷链接的正则表达式

正则表达式

/^thunderx?:\/\/[a-zA-Z\d]+=$/

解析

  • ^:匹配字符串的开始。
  • thunderx?:匹配字符串 thunder 后面跟一个可选的 x。即可以匹配 thunderthunderx
    • thunder:固定匹配 thunder 字符串。
    • x?:表示 x 是可选的,匹配 0 次或 1 次 x
  • ://:匹配 :// 字符串。
  • [a-zA-Z\d]+:匹配一个或多个字母或数字字符。
    • [a-zA-Z\d]:字符类,匹配一个字母(大写或小写)或数字字符(0-9)。
    • +:量词,匹配前面的字符类 1 次或多次。
  • =:匹配等号 = 字符。
  • $:匹配字符串的结束。

总体解释

  • 这个正则表达式用于匹配以 thunder://thunderx:// 开头,后面跟一个或多个字母或数字字符,并以等号 = 结尾的字符串。

示例

匹配成功的示例:

  • "thunder://abc123="
  • "thunderx://A1b2C3="
  • "thunder://XYZ456="
  • "thunderx://789abc="

匹配失败的示例:

  • "thunder://abc123"(没有等号 = 结尾)
  • "thunderx://A1b2C3"(没有等号 = 结尾)
  • "thunder:/abc123="(缺少一个斜杠 /
  • "thunderxx://abc123="(多一个 x
  • "thunderx://abc 123="(包含空格)
  • "thunderx://abc!123="(包含非法字符 !

Python

import re

pattern = r'^thunderx?:\/\/[a-zA-Z\d]+=$'
test_strings = ["thunder://abc123=", "thunderx://A1b2C3=", "thunder://XYZ456=", "thunderx://789abc=", "thunder://abc123", "thunderx://A1b2C3", "thunder:/abc123=", "thunderxx://abc123=", "thunderx://abc 123=", "thunderx://abc!123="]

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

JavaScript

const pattern = /^thunderx?:\/\/[a-zA-Z\d]+=$/;
const testStrings = ["thunder://abc123=", "thunderx://A1b2C3=", "thunder://XYZ456=", "thunderx://789abc=", "thunder://abc123", "thunderx://A1b2C3", "thunder:/abc123=", "thunderxx://abc123=", "thunderx://abc 123=", "thunderx://abc!123="];

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 = "^thunderx?:\\/\\/[a-zA-Z\\d]+=$";
        String[] testStrings = {"thunder://abc123=", "thunderx://A1b2C3=", "thunder://XYZ456=", "thunderx://789abc=", "thunder://abc123", "thunderx://A1b2C3", "thunder:/abc123=", "thunderxx://abc123=", "thunderx://abc 123=", "thunderx://abc!123="};
        
        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 = @"^thunderx?:\/\/[a-zA-Z\d]+=$";
        string[] testStrings = {"thunder://abc123=", "thunderx://A1b2C3=", "thunder://XYZ456=", "thunderx://789abc=", "thunder://abc123", "thunderx://A1b2C3", "thunder:/abc123=", "thunderxx://abc123=", "thunderx://abc 123=", "thunderx://abc!123="};
        
        foreach (string testString in testStrings) {
            if (Regex.IsMatch(testString, pattern)) {
                Console.WriteLine($"'{testString}': 匹配成功");
            } else {
                Console.WriteLine($"'{testString}': 匹配失败");
            }
        }
    }
}

PHP

<?php
$pattern = '/^thunderx?:\/\/[a-zA-Z\d]+=$/';
$testStrings = ["thunder://abc123=", "thunderx://A1b2C3=", "thunder://XYZ456=", "thunderx://789abc=", "thunder://abc123", "thunderx://A1b2C3", "thunder:/abc123=", "thunderxx://abc123=", "thunderx://abc 123=", "thunderx://abc!123="];

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

Ruby

pattern = /^thunderx?:\/\/[a-zA-Z\d]+=$/
test_strings = ["thunder://abc123=", "thunderx://A1b2C3=", "thunder://XYZ456=", "thunderx://789abc=", "thunder://abc123", "thunderx://A1b2C3", "thunder:/abc123=", "thunderxx://abc123=", "thunderx://abc 123=", "thunderx://abc!123="]

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