我主要以 yyyy-MM-dd 和 yyyy-MM-dd hh:mm:ss 的形式约会.我想模式匹配 yyyy-MM-dd hh:mm:ss .
I have dates mainly in the forms of yyyy-MM-dd and yyyy-MM-dd hh:mm:ss. I want to pattern match yyyy-MM-dd hh:mm:ss.
为此目的编写正则表达式很简单吗?我是regex的新手,所以不胜感激任何资源达人都可以让我快速入门吗?
Is it trivial to write an regex for this purpose? I am new to regex so would appreciate any resource tat could get me up to speed?
推荐答案如果要在字符串中查找这些日期符号,则正则表达式确实是一个不错的选择.您可以使用此正则表达式:
If you are trying to find these date notations in a String, then regexes is indeed a good choice. You could use this regex:
\b\d{4}-\d{2}-\d{2} \d{1,2}:\d{2}:\d{2}\b在此处查看操作: rubular/r/qZOTsUikbo .注意:这与"日期"匹配,例如以及 9999-99-99 99:99:99 .如果这对您来说是个问题,则可以在找到潜在的字符串后对其进行验证.
See it in action here: rubular/r/qZOTsUikbo. Note: this matches "dates" like 9999-99-99 99:99:99 as well. If that is a problem for you, you could verify them after you found the potential Strings.
如果要解析它们,请使用SimpleDateFormat.
If you want to parse them, then use a SimpleDateFormat.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = sdf.parse("2013-12-24 18:31:20");更多推荐
正则表达式,用于SimpleDateFormat字符串
发布评论