[\u4e00-\u9fa5]
[\u4e00-\u9fa5]
:匹配任意一个Unicode范围在4E00
到9FA5
之间的中文字符。import re
pattern = r'[\u4e00-\u9fa5]'
test_strings = ["你好", "Hello", "世界"]
for test_string in test_strings:
if re.search(pattern, test_string):
print(f"'{test_string}': 包含中文字符")
else:
print(f"'{test_string}': 不包含中文字符")
const pattern = /[\u4e00-\u9fa5]/;
const testStrings = ["你好", "Hello", "世界"];
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]";
String[] testStrings = {"你好", "Hello", "世界"};
for (String testString : testStrings) {
if (Pattern.compile(pattern).matcher(testString).find()) {
System.out.println("'" + testString + "': 包含中文字符");
} else {
System.out.println("'" + testString + "': 不包含中文字符");
}
}
}
}
using System;
using System.Text.RegularExpressions;
class Program {
static void Main() {
string pattern = "[\u4e00-\u9fa5]";
string[] testStrings = {"你好", "Hello", "世界"};
foreach (string testString in testStrings) {
if (Regex.IsMatch(testString, pattern)) {
Console.WriteLine($"'{testString}': 包含中文字符");
} else {
Console.WriteLine($"'{testString}': 不包含中文字符");
}
}
}
}
<?php
$pattern = '/[\x{4e00}-\x{9fa5}]/u';
$testStrings = ["你好", "Hello", "世界"];
foreach ($testStrings as $testString) {
if (preg_match($pattern, $testString)) {
echo "'$testString': 包含中文字符\n";
} else {
echo "'$testString': 不包含中文字符\n";
}
}
?>
pattern = /[\u4e00-\u9fa5]/
test_strings = ["你好", "Hello", "世界"]
test_strings.each do |test_string|
if pattern.match?(test_string)
puts "'#{test_string}': 包含中文字符"
else
puts "'#{test_string}': 不包含中文字符"
end
end