[Code]Regex Collection

Regex Collection

本篇要來收集網路上或自製的Regex語法. 不定期的更新!!

2010/11/9

 

					String.prototype.trim = function() {  return this.replace(/^\s+|\s+$/g, '');  }

出處: Create Trim function in javascript by shankeyshankey

2010/11/17

					String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}

出處: Trim, LTrim and RTrim Functions using Regular Expressions by Sunasara Imdadhusen
					private final static String pattern = "/(\\w*)(\\()(.*)(\\))/";
    
    private String functionName;
    private String strParams;
    private Iterator params;
    
    private String current;
    private int count;
    /**
     * 
     */
    public FunctionContext(String context) {
        super();    
        // parse the name of function and the full string of parameters 
        Perl5Util util = new Perl5Util();
        PatternMatcherInput matcherInput = new PatternMatcherInput(context);
        boolean isMatch = util.match(pattern,matcherInput);
        if(!isMatch)
            throw new ASDException("Context format error. " + context);        
        MatchResult result = util.getMatch();        
        this.functionName = result.group(1);
        this.strParams = result.group(3);
        // parse full string of parameters to iterator
        ArrayList paramlist = parseParams(strParams);
        this.params = paramlist.iterator();
        this.count = paramlist.size(); // 參數總筆數
    }
    
    /**
     * 
     * @param strParams
     * @return
     */
    private ArrayList parseParams(String strParams){
        ArrayList paramlist = new ArrayList();
        
        String[] ary = StringUtility.split(strParams,",");
        StringBuffer b = new StringBuffer();
        int touch = 0;
        for (int i=0;i0){
                
            }else{ // 若前後括後match, 則填入params, 順便把最後的逗號trim掉               
                paramlist.add(b.substring(0,b.length()-1)); 
                b = new StringBuffer();
            }
        }  
        return paramlist;
        
        
    }
	

出處: FunctionContext in Java by me
					"^(?i:true|yes|y|1|ok|是)$"

出處: ParseBoolean by me

Oracle  檢查身份證字號/統一編號

 

SELECT case when REGEXP_LIKE('A123456789', '^[a-zA-Z]\d{9}$') then 'true' else 'false' end as IS_PTY_ID  FROM DUAL;
SELECT case when REGEXP_LIKE('23456789', '^\d{8}$') then 'true' else 'false' end as IS_COR_ID FROM DUAL;
 

不可含有 % , < , > = ... 等字元

^((?!(\%|<|>|=)).)*$

出處: http://stackoverflow.com/questions/406230/regular-expression-to-match-string-not-containing-a-word

處理 SQL 中的 ORDER BY 可能的SQL Injection 問題

((,)?(\s)*\b(IP_ID|CUST_NAME)\b(\s)*(\b(ASC|DESC)(,)?\b)?)+