包名匹配

2024-06-23 21:05:52 464
正则匹配包名的正则表达式,如java包名、app包名、鸿蒙包名等

正则表达式

/^([a-zA-Z_]\w*)+([.][a-zA-Z_]\w*)+$/

解析

  • ^:匹配字符串的开始。
  • *([a-zA-Z_]\w)+**:
    • [a-zA-Z_]:匹配一个字母(大小写均可)或下划线。
    • \w*:匹配零个或多个单词字符(字母、数字或下划线)。
    • *([a-zA-Z_]\w)+**:匹配一个或多个由字母或下划线开头,后跟零个或多个单词字符的字符串。
  • *([.][a-zA-Z_]\w)+**:
    • [.]:匹配一个点字符。
    • [a-zA-Z_]:匹配一个字母(大小写均可)或下划线。
    • \w*:匹配零个或多个单词字符。
    • *([.][a-zA-Z_]\w)+**:匹配一个或多个由点字符开头,后跟一个字母或下划线和零个或多个单词字符的字符串。
  • $:匹配字符串的结束。

示例

匹配成功的示例:

  • "file.name"
  • "class.method"
  • "package.class.method"

匹配失败的示例:

  • "file."(点后没有字符)
  • ".file"(点前没有字符)
  • "file..name"(连续两个点)

用法

Python

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}': 匹配失败")

JavaScript

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}': 匹配失败`);
    }
});

Java

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 + "': 匹配失败");
            }
        }
    }
}

C#

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

<?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";
    }
}
?>

Ruby

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