Java上的正则表达式格式验证 [英] Regex date format validation on Java

查看:287
本文介绍了Java上的正则表达式格式验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道是否有一种方式(可能使用正则表达式)来验证Java桌面应用程序上的输入是否为一个字符串:YYYY-MM-DD。

I'm just wondering if there is a way (maybe with regex) to validate that an input on a Java desktop app is exactly an string formated as: "YYYY-MM-DD".

我搜索过但没有成功。

谢谢

推荐答案

使用以下正则表达式:

^\d{4}-\d{2}-\d{2}$

if (str.matches("\\d{4}-\\d{2}-\\d{2}")) {
    ...
}

使用匹配方法,锚点 ^ $ (字符串的开头和结尾,分别)隐含存在。

With the matches method, the anchors ^ and $ (beginning and end of string, respectively) are present implicitly.

这篇关于Java上的正则表达式格式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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