/^(((ht|f)tps?):\/\/)?([^!@#$%^&*?.\s-]([^!@#$%^&*?.\s]{0,63}[^!@#$%^&*?.\s])?\.)+[a-z]{2,6}\/?/
http
, https
, ftp
, ftps
。://
。!@#$%^&*?.
)和空格。.
。/
(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"
(主机名中有空格)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}': 匹配失败")
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}': 匹配失败`);
}
});
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 + "': 匹配失败");
}
}
}
}
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
$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";
}
}
?>
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
这些关键词可以帮助用户