Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Time Regex expression  RSS feed

 
Ranch Hand
Posts: 72
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,
Can anyone help me to write regex to match with String below:

24/02/2019 10:46



for mat date time is DD/MM/YYYY HH:MM

This is urgent task of mine. I have searched it but no expected result.
Please help me
 
Sheriff
Posts: 6738
466
BSD Linux Mac OS X VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there are some results which do what you need, at least seem to be very close --> here
 
Saloon Keeper
Posts: 10206
216
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you just want to match the string or do you want to parse it?

If you want to match it, read the documentation of the Pattern class.

If you want to parse it, read the documentation of the DateTimeFormatter class.

If you're stuck, then tell us what you've tried and what part of the documentation you don't understand.
 
lowercase baba
Posts: 12745
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to be more specific.  something like this would match that string:

.*

if you want to get more precise, you could do it on digits and literals, i.e.

\d\d/\d\d....etc

if you then further want to validate the characters before the first slash resolve to a value less than 32, or are valid against the month specified (i.e. no "30" if the month is "02"), then you need more than just a regex.
 
Bartender
Posts: 20721
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm with Stephan.

Unless you have to 100% match that pattern, using the DateTimeFormatter is a much more useful solution. If the input matches the date/time pattern, you get a free conversion to a timestamp as a side effect. If it fails to match, you get an Exception.
 
fred rosenberger
lowercase baba
Posts: 12745
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that sort of brings the question around do "do you need to know how to write a regex" or "do you need to match this string and/or do something to it...".  The details matter when it comes to getting the best possible help.
 
Marshal
Posts: 24461
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's pretty hard to write a regex which will reject February 29 except for a leap year -- but then that wasn't exactly one of the stated requirements.
 
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . a regex which will reject February 29 except for a leap year . . . .

That sounds like something outwith the scope of a regular grammar. But don't worry; I think LocalDate will throw lots of exceptions if you try to tell it today is 29th February 2019.
 
Paul Clapham
Marshal
Posts: 24461
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I think LocalDate will throw lots of exceptions if you try to tell it today is 29th February 2019.



So do I. That's why I think you should use LocalDate rather than a regex.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!