- package com.onewaveinc.icms.record.periodicity.regexp;
- /**
- * csv单元格正则匹配
- *
- */
- public class RegExp {
- private static final String SPECIAL_CHAR_A = "[^\\",\\\\n ]";
- private static final String SPECIAL_CHAR_B = "[^\\",\\\\n]";
- /**
- * 正则表达式。
- *
- * @return 匹配csv文件里最小单位的正则表达式。
- */
- public String getRegExp() {
- StringBuffer strRegExps = new StringBuffer();
- strRegExps.append("\\"((");
- strRegExps.append(SPECIAL_CHAR_A);
- strRegExps.append("*[,\\\\n ])*(");
- strRegExps.append(SPECIAL_CHAR_A);
- strRegExps.append("*\\"{2})*)*");
- strRegExps.append(SPECIAL_CHAR_A);
- strRegExps.append("*\\"[ ]*,[ ]*");
- strRegExps.append("|");
- strRegExps.append(SPECIAL_CHAR_B);
- strRegExps.append("*[ ]*,[ ]*");
- strRegExps.append("|\\"((");
- strRegExps.append(SPECIAL_CHAR_A);
- strRegExps.append("*[,\\\\n ])*(");
- strRegExps.append(SPECIAL_CHAR_A);
- strRegExps.append("*\\"{2})*)*");
- strRegExps.append(SPECIAL_CHAR_A);
- strRegExps.append("*\\"[ ]*");
- strRegExps.append("|");
- strRegExps.append(SPECIAL_CHAR_B);
- strRegExps.append("*[ ]*");
- return strRegExps.toString();
- }
- /**
- * 正则表达式匹配时间格式的日期hh:mm:ss
- *
- * @return
- */
- public String matchHMSRegExp() {
- StringBuffer strRegExps = new StringBuffer();
- strRegExps.append("^(20|21|22|23|[0-1][0-9]):[0-5][0-9]:[0-5][0-9]$");
- return strRegExps.toString();
- }
- /**
- * 正则表达式匹配时间格式的日期h:mm:ss
- * @return
- */
- public String matchhMSRegExp(){
- StringBuffer strRegExps = new StringBuffer();
- strRegExps.append("^[1-9]:[0-5][0-9]:[0-5][0-9]$");
- return strRegExps.toString();
- }
- /**
- * 正则表达式匹配时间格式的日期yyyy-MM-dd
- *
- * @return
- */
- public String matchYMDRegExp() {
- StringBuffer strRegExps = new StringBuffer();
- strRegExps
- .append("^((((19|20)\\\\d{2})-(0?[13-9]|1[012])-(0?[1-9]|[12]\\\\d|30))|(((19|20)\\\\d{2})-(0?[13578]|1[02])-31)|(((19|20)\\\\d{2})-0?2-(0?[1-9]|1\\\\d|2[0-8]))|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0?2-29))$");
- return strRegExps.toString();
- }
- public static void main(String[] args) {
- RegExp re = new RegExp();
- String regExp = re.matchYMDRegExp();
- Pattern pCells = Pattern.compile(regExp);
- Matcher m = pCells.matcher("2013-09-24");
- if (!m.matches()) {
- System.out.println("不匹配");
- }else{
- System.out.println("匹配");
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0412201411181.html
来源: http://www.codesnippet.cn/detail/0412201411181.html