ECM
ECM
总版主
总版主
  • 社区居民
  • 忠实会员
  • 原创写手
阅读:802回复:0

Fetch the Date from a String using Regex [closed]

楼主#
更多 发布于:2016-05-30 21:31
i need an regular expression to extract only the date from this kind of strings, regex to be used in java. Friendlies : International U17 W - 16/04/2014 Japan : J2-League - 20/04/2014 2014 FIFA WORLD CUP IN BRAZIL - 12/06/2014 anyone can help???
answer(解决方案):

From the comments above(that you do not bother about the date's validness), Here is what you are looking for: String str = "Friendlies : International U17 W - 16/04/2014";
str = str.replaceAll(".*(\\d\\d?/\\d\\d?/\\d{4}).*", "$1");
System.out.println(str);
 In this example, the code is capturing the date using the group (...) and replacing the whole string with the captured group, which ensures to have the date. But, a better approach will be: String str = "Friendlies : International U17 W - 16/04/2014";
Pattern pattern = Pattern.compile("(\\d\\d?/\\d\\d?/\\d{4})");
Matcher m = pattern.matcher(str);
if(m.find()) {
    System.out.println(m.group(1));
}

 

 

 

 

 

 

 

异常中心网是一家专门收集整理程序员编程过程中遇到的常见异常(exception)以及各种异常问答中心的网站。异常中心网旨在,减少程序员在编码遇到异常,处理各种异常时间和痛苦,让程序员能更愉快的、快速的定位异常并查找对应的异常解决方案。异常中心网诚心打造最完美的编程社区为程序员用户服务,努力成为最好的程序员乐园程序员社区程序异常中心程序bug中心异常问答中心

 

喜欢0 评分0
游客

返回顶部