正则表达式,用于SimpleDateFormat字符串 [英] Regex for SimpleDateFormat string

查看:82
本文介绍了正则表达式,用于SimpleDateFormat字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我主要以 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

在此处查看操作: http://rubular.com/r/qZOTsUikbo .注意:这与"日期"匹配,例如
以及 9999-99-99 99:99:99 .如果这对您来说是个问题,则可以在找到潜在的字符串后对其进行验证.

See it in action here: http://rubular.com/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字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆