java利用正则表达式提取年月日数据

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<String> getMatcherStrs(String content,String sPattern){
Pattern p = Pattern.compile(sPattern);
Matcher m = p.matcher(content);
List<String> result=new ArrayList<String>();
while(m.find()){
result.add(m.group());
}
return result;
}

/** * 获取单个匹配的字符串 * @param content * @param sPattern * @return */
public static String getMatcherStr(String content,String sPattern){
List<String> strs=getMatcherStrs( content, sPattern);
if(strs.size()>0){
return strs.get(0);
}else{
return "";
}

}

/** * 获取匹配的所有字符串 * @param content * @param sPattern * @return */
public static List<String> getMatcherStrsAmong(String content,String sPattern){
Pattern p = Pattern.compile(sPattern);
Matcher m = p.matcher(content);
List<String> result=new ArrayList<String>();
while(m.find()){
result.add(m.group(1));
}
return result;
}
public static String getMatcherStrAmong(String content,String sPattern){
List<String> strs=getMatcherStrsAmong(content,sPattern);
if(strs.size()>0){
return strs.get(0);
}else{
return "";
}
}

/** * 获取所有匹配的日期 * @param content * @param splitStr * @return */
public static List<String> 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="&nbsp;&nbsp;发布时间:2016-06-06 15:51:03 ";
getDate(str,"-");
}


}

转载自:http://blog.csdn.net/ycb1689/article/details/52131691?locationNum=1&fps=1

    原文作者:u010466329
    原文地址: https://blog.csdn.net/u010466329/article/details/78404162
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞