作用

匹配任何大写字母

代码

public class Test {

    private static final String REGEX = "\\p{javaUpperCase}";
    private static final String INPUT = "Bb1C2 \tc!";
    
    public static void main(String[] args) {
        // create a pattern
        Pattern pattern = Pattern.compile(REGEX);
        // get a matcher object
        Matcher matcher = pattern.matcher(INPUT);
        while (matcher.find()) {
            //Prints the start index of the match.
            System.out.println("Match String start(): " + matcher.start());
        }
    }
}

结果

Match String start(): 0
Match String start(): 3

用法

import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validator;

/**
 * Bean 工具类
 * 
 * @author ruoyi
 */
public class BeanUtils extends org.springframework.beans.BeanUtils
{
    /** Bean方法名中属性名开始的下标 */
    private static final int BEAN_METHOD_PROP_INDEX = 3;

    /** * 匹配getter方法的正则表达式 */
    private static final Pattern GET_PATTERN = Pattern.compile("get(\\p{javaUpperCase}\\w*)");

    /** * 匹配setter方法的正则表达式 */
    private static final Pattern SET_PATTERN = Pattern.compile("set(\\p{javaUpperCase}\\w*)");

    /**
     * 获取对象的setter方法。
     * 
     * @param obj 对象
     * @return 对象的setter方法列表
     */
    public static List<Method> getSetterMethods(Object obj)
    {
        // setter方法列表
        List<Method> setterMethods = new ArrayList<Method>();

        // 获取所有方法
        Method[] methods = obj.getClass().getMethods();

        // 查找setter方法

        for (Method method : methods)
        {
            Matcher m = SET_PATTERN.matcher(method.getName());
            if (m.matches() && (method.getParameterTypes().length == 1))
            {
                setterMethods.add(method);
            }
        }
        // 返回setter方法列表
        return setterMethods;
    }

    /**
     * 获取对象的getter方法。
     * 
     * @param obj 对象
     * @return 对象的getter方法列表
     */

    public static List<Method> getGetterMethods(Object obj)
    {
        // getter方法列表
        List<Method> getterMethods = new ArrayList<Method>();
        // 获取所有方法
        Method[] methods = obj.getClass().getMethods();
        // 查找getter方法
        for (Method method : methods)
        {
            Matcher m = GET_PATTERN.matcher(method.getName());
            if (m.matches() && (method.getParameterTypes().length == 0))
            {
                getterMethods.add(method);
            }
        }
        // 返回getter方法列表
        return getterMethods;
    }
}

拓展 \p{javaLowerCase}

// 作用:匹配所有小写字母
// 结果:
// Match String start(): 1
// Match String start(): 7
public class Test {
    private static final String REGEX = "\\p{javaLowerCase}";
    private static final String INPUT = "Bb1C2 \tc!";

    public static void main(String[] args) {
        // create a pattern
        Pattern pattern = Pattern.compile(REGEX);
        // get a matcher object
        Matcher matcher = pattern.matcher(INPUT);
        while (matcher.find()) {
            //Prints the start index of the match.
            System.out.println("Match String start(): " + matcher.start());
        }
    }
}
Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐