• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Time Regex expression

 
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15529
364
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 27808
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic