日期:2014-05-18 浏览次数:20990 次
package com.tur.demo;
public class Hello {
public static void main(String[] args) {
String m = "public List<int> foo(Type name, ...)";
// 名字,模板,类型的正则
String namePattern = "[_a-zA-Z]+\\w*";
String templatePattern = "(<[_a-zA-Z]+\\w*(, [_a-zA-Z]+\\w*)*>)?";
String typePattern = namePattern + templatePattern;
// 参数的正则
String argPatternZero = ""; // 无参数
String argPatternDynamic = "..."; // 动态参数
String argPattern = typePattern + " " + namePattern; // 一个参数
String argPatternOne = argPattern + "(, ...)?"; // 一个参数 + 动态参数
String argPatternMulti = argPattern + "(, " + argPattern + ")*(, ...)?"; // 多个参数 + 动态参数
String argsPattern = "(" + argPatternZero + "
------解决方案--------------------
" + argPatternDynamic + "
------解决方案--------------------
" + argPatternOne + "
------解决方案--------------------
" + argPatternMulti + ")";
// 方法签名的正则
String methodSignaturePattern = "(public
------解决方案--------------------
protected
------解决方案--------------------
private)( static)?( final)? " +
typePattern + " " + namePattern + "\\(" + argsPattern + "\\)";
// 每个部分的正则测试
System.out.println("List<int, Object>".matches(typePattern));
System.out.println("".matches(argPatternZero));
System.out.println("...".matches(argPatternDynamic));
System.out.println(argPatternOne);