简体   繁体   English

正则表达式模式,用于从包含由定界符分隔的日期的字符串中删除.0

[英]Regex pattern for removing .0 from a string containing date separated by Delimiter

I have a string which looks something like this: 我有一个看起来像这样的字符串:

"2013-10-19 22:21:21#2013-10-23 16:17:19#2013-10-25 13:15:14.0#2013-10-19 08:11:34.0#2013-10-23 16:17:19#"

I need to remove the .0 from the end of the date. 我需要从日期末尾删除.0。 ie 2013-10-25 13:15:14.0 should be 2013-10-25 13:15:14 2013-10-25 13:15:14.0 should be 2013-10-25 13:15:14

What can be the possible regex pattern for this.. 可能的正则表达式模式是什么。

If that is your string and there's no other data inside, you can use a simple replace; 如果那是您的字符串,并且里面没有其他数据,则可以使用简单的替换; no need for any regex: 无需任何正则表达式:

String str = "2013-10-19 22:21:21#2013-10-23 16:17:19#2013-10-25 13:15:14.0#2013-10-19 08:11:34.0#2013-10-23 16:17:19#";
String res = str.replace(".0", "");
System.out.println(res);

Output: 输出:

2013-10-19 22:21:21#2013-10-23 16:17:19#2013-10-25 13:15:14#2013-10-19 08:11:34#2013-10-23 16:17:19# - See more at: http://ideone.com/WrDbqS#sthash.iQsWV8gp.dpuf

ideone demo ideone演示

You can use: 您可以使用:

str = str.replaceAll("\\.0(?=#)", "");

This will replace .0 with an empty string if it is followed by a # 如果后跟#则它将用空字符串替换.0

当出现在#或字符串末尾时,将删除(用空白代替) .0

str = str.replaceAll("\\.0(?=#|$)", "");

尝试这个

str = str.replaceAll("(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2})\\.\\d+", "$1");

Without regex you can do as follows. 没有regex您可以执行以下操作。

 String str="2013-10-19 22:21:21#2013-10-23 16:17:19#2013-10-25 13:15:14.0#2013-10-19 08:11:34.0#2013-10-23 16:17:19#";
   String[] dates=str.split("#");
   for(String i:dates){
       if(i.lastIndexOf('0')==(i.length()-1)){
           System.out.println(i.replaceAll(".0",""));
       }else {
           System.out.println(i);
       }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM