/^([a-zA-Z_]\w*)+([.][a-zA-Z_]\w*)+$/
"file.name"
"class.method"
"package.class.method"
"file."
(点后没有字符)".file"
(点前没有字符)"file..name"
(连续两个点)import re
pattern = r'^([a-zA-Z_]\w*)+([.][a-zA-Z_]\w*)+$'
test_strings = ["file.name", "class.method", "package.class.method", "file.", ".file", "file..name"]
for test_string in test_strings:
if re.match(pattern, test_string):
print(f"'{test_string}': 匹配成功")
else:
print(f"'{test_string}': 匹配失败")
const pattern = /^([a-zA-Z_]\w*)+([.][a-zA-Z_]\w*)+$/;
const testStrings = ["file.name", "class.method", "package.class.method", "file.", ".file", "file..name"];
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 = "^([a-zA-Z_]\\w*)+([.][a-zA-Z_]\\w*)+$";
String[] testStrings = {"file.name", "class.method", "package.class.method", "file.", ".file", "file..name"};
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 = "^([a-zA-Z_]\\w*)+([.][a-zA-Z_]\\w*)+$";
string[] testStrings = {"file.name", "class.method", "package.class.method", "file.", ".file", "file..name"};
foreach (string testString in testStrings) {
if (Regex.IsMatch(testString, pattern)) {
Console.WriteLine($"'{testString}': 匹配成功");
} else {
Console.WriteLine($"'{testString}': 匹配失败");
}
}
}
}
<?php
$pattern = '/^([a-zA-Z_]\w*)+([.][a-zA-Z_]\w*)+$/';
$testStrings = ["file.name", "class.method", "package.class.method", "file.", ".file", "file..name"];
foreach ($testStrings as $testString) {
if (preg_match($pattern, $testString)) {
echo "'$testString': 匹配成功\n";
} else {
echo "'$testString': 匹配失败\n";
}
}
?>
pattern = /^([a-zA-Z_]\w*)+([.][a-zA-Z_]\w*)+$/
test_strings = ["file.name", "class.method", "package.class.method", "file.", ".file", "file..name"]
test_strings.each do |test_string|
if pattern.match?(test_string)
puts "'#{test_string}': 匹配成功"
else
puts "'#{test_string}': 匹配失败"
end
end