![]() |
In this article, we will explore how to use regex to validate the used time format HH:mm:ss. Regular expressions (regex) offer a method, for validating time formats ensuring that they follow patterns which is mentioned below in the article. How to Validate a Time Format (HH:mm:ss) Using Regex?When testing a time string, against this pattern the regular expression engine tries to match each part of the pattern to its part, in the string. If the entire string perfectly matches the pattern, it is considered a time format in HH:mm:ss. If any part of the string does not follow the pattern, it is considered invalid. Regex Pattern:^([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$ In the below example, we will check whether the given time “18:35:40” is valid or not:Java
Output
18:35:40 is a valid time format. Explaination of the above Program:In the above Program we are comparing the String with the mentioned regex. And the regex is explained below:
|
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |