2015年9月14日 星期一

Java Method for Regular Expression


以下 Method 為判斷字串是否符合所設定之 Regular Expression

    /**
     * Get the match result between string and regular expression pattern string
     *
     * @param patternStr - Regular expression pattern string.
     * @param testString - String for testing with match pattern.
     * @return true/false: String is match (true), or is not match (false).
     */
    public static boolean isRegexMatch(String patternStr, String testString) {
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(testString);
       
        return matcher.matches();
    }