/^(?:[\u4e00-\u9fa5·]{2,16})$/
\u4e00-\u9fa5
:匹配 Unicode 范围内的中文字符(包含简体和繁体)。·
:匹配中间点字符(可能用于人名)。"张三"
"李四·王五"
"王小明"
"赵钱孙李"
"陈大文·小文"
"A张三"
(包含非中文字符)"张三1"
(包含非中文字符)"张"
(长度不足 2)"王小明李四赵钱孙陈大文周武郑王张李黄"
(长度超过 16)import re
pattern = r'^[\u4e00-\u9fa5·]{2,16}$'
test_strings = ["张三", "李四·王五", "王小明", "赵钱孙李", "陈大文·小文", "A张三", "张三1", "张", "王小明李四赵钱孙陈大文周武郑王张李黄"]
for test_string in test_strings:
if re.match(pattern, test_string):
print(f"'{test_string}': 匹配成功")
else:
print(f"'{test_string}': 匹配失败")
const pattern = /^[\u4e00-\u9fa5·]{2,16}$/;
const testStrings = ["张三", "李四·王五", "王小明", "赵钱孙李", "陈大文·小文", "A张三", "张三1", "张", "王小明李四赵钱孙陈大文周武郑王张李黄"];
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 = "^[\\u4e00-\\u9fa5·]{2,16}$";
String[] testStrings = {"张三", "李四·王五", "王小明", "赵钱孙李", "陈大文·小文", "A张三", "张三1", "张", "王小明李四赵钱孙陈大文周武郑王张李黄"};
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 = @"^[\u4e00-\u9fa5·]{2,16}$";
string[] testStrings = {"张三", "李四·王五", "王小明", "赵钱孙李", "陈大文·小文", "A张三", "张三1", "张", "王小明李四赵钱孙陈大文周武郑王张李黄"};
foreach (string testString in testStrings) {
if (Regex.IsMatch(testString, pattern)) {
Console.WriteLine($"'{testString}': 匹配成功");
} else {
Console.WriteLine($"'{testString}': 匹配失败");
}
}
}
}
<?php
$pattern = '/^[\u4e00-\u9fa5·]{2,16}$/';
$testStrings = ["张三", "李四·王五", "王小明", "赵钱孙李", "陈大文·小文", "A张三", "张三1", "张", "王小明李四赵钱孙陈大文周武郑王张李黄"];
foreach ($testStrings as $testString) {
if (preg_match($pattern, $testString)) {
echo "'$testString': 匹配成功\n";
} else {
echo "'$testString': 匹配失败\n";
}
}
?>
pattern = /^[\u4e00-\u9fa5·]{2,16}$/
test_strings = ["张三", "李四·王五", "王小明", "赵钱孙李", "陈大文·小文", "A张三", "张三1", "张", "王小明李四赵钱孙陈大文周武郑王张李黄"]
test_strings.each do |test_string|
if pattern.match?(test_string)
puts "'#{test_string}': 匹配成功"
else
puts "'#{test_string}': 匹配失败"
end
end