자바 정규 표현 식 으로 년 월 일 데이터 추출

5192 단어
package com.amarsoft.court.app.util;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


/**
 *           
 * @author ytwang
 *
 */
public class MatcherAssist {

/**
*           
* @param content
* @param sPattern
* @return
*/
public static List getMatcherStrs(String content,String sPattern){
Pattern p = Pattern.compile(sPattern);
Matcher m = p.matcher(content);
List result=new ArrayList();
while(m.find()){
result.add(m.group());
}
return result;
}

/**
*           
* @param content
* @param sPattern
* @return
*/
public static String getMatcherStr(String content,String sPattern){
List strs=getMatcherStrs( content, sPattern);
if(strs.size()>0){
return strs.get(0);
}else{
return "";
}

}

/**
*           
* @param content
* @param sPattern
* @return
*/
public static List getMatcherStrsAmong(String content,String sPattern){
Pattern p = Pattern.compile(sPattern);
Matcher m = p.matcher(content);
List result=new ArrayList();
while(m.find()){
result.add(m.group(1));
}
return result;
}
public static String getMatcherStrAmong(String content,String sPattern){
List strs=getMatcherStrsAmong(content,sPattern);
if(strs.size()>0){
return strs.get(0);
}else{
return "";
}
}

/**
*          
* @param content
* @param splitStr
* @return
*/
public static List getDates(String content,String splitStr){
return getMatcherStrs(content,"\\d{4}\\"+splitStr+"\\d{1,2}\\"+splitStr+"\\d{1,2}");
}
/**
*           
* @param content
* @param splitStr
* @return
*/
public static String getDate(String content,String splitStr){
return getDates(content,splitStr).get(0);
}

public static void main(String[] args){
String str="      :2016-06-06 15:51:03 ";
getDate(str,"-");
}


}

다음으로 전송:http://blog.csdn.net/ycb1689/article/details/52131691?locationNum=1&fps=1

좋은 웹페이지 즐겨찾기