-1

I'm trying to create a regular expression for the following date/time format:

1970-09-29T00:00:00.000+0000

My current regex is:

[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{4}:[0-9]{2}:[0-9]{2}+[0-9]+
ctwheels
  • 21,901
  • 9
  • 42
  • 77
SSK
  • 53
  • 1
  • 9
  • 1
    Great question, maybe show us where your regex went wrong? [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) [How to create a Minimal, Complete, and Verifiable example?](https://stackoverflow.com/help/mcve) – ctwheels Oct 25 '17 at 18:41
  • 1
    @Sindhu Please give this a quick read, particularly the 2nd bullet point under "How to ask" https://stackoverflow.com/tags/regex/info – CAustin Oct 25 '17 at 18:43
  • The most important thing to note is: you must express clearly your intent. For what you need a regular expression in this case? What are you trying to find/match in your date string? – Gruber Oct 25 '17 at 18:48
  • I am currently trying to use [0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{4}:[0-9]{2}:[0-9]{2}+[0-9]+ – SSK Oct 25 '17 at 18:51
  • Unless you're looking for _VALID_ dates, use the general purpose `\d` like what you have. Except you have to escape the `+` metachar –  Oct 25 '17 at 18:55
  • `\d+-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+\+\d+`? You had `{4}` coded for the first time chunk, you didn't have a `.` for the dot separator, and `+` wasn't escaped and you were missing the last set of digits – ctwheels Oct 25 '17 at 18:58
  • Your regex requires 4 digits immediately after the `T`; your example has 2. You don't capture the `.` or the `+` (which you need to escape), and it's not anchored at the beginning or end. What exactly are you trying to validate? Do you expect your regex to reject a month or day of `99`? I suggest having the regex just validate that you have decimal digits in the right layout, and use other logic to check the values. Finally, and probably most important, you haven't really asked a question. – Keith Thompson Oct 25 '17 at 19:19
  • Thanks ctwheels. The regex that you provided resolved my problem. – SSK Oct 25 '17 at 20:30

2 Answers2

0

Might be missing some digits there.
The regex should be ^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}\+\d{4}$

  • Check it. How is it now? –  Oct 25 '17 at 19:02
  • Why not leave it, I like the `\d+` part. –  Oct 25 '17 at 19:03
  • I meant my comment on this answer. You can copy my code from the comments under the question and add it to your answer if you'd like. No point in adding two different answers. The amount of issues people will face in the year 10000 kills me haha – ctwheels Oct 25 '17 at 19:04
  • True that. But, leap year, now that .. `(?:1(?:5(?:8[48]|9[26])|[6-9](?:0[48]|[13579][26]|[2468][048]))|[2-9](?:[0-9](?:0[48]|[13579][26]|[2468][048]))|(?:(?:16|[2468][048]|[3579][26])00))` –  Oct 25 '17 at 19:07
  • Try the leap year I wrote [here](https://stackoverflow.com/a/46414732/3600709) instead. `(?:\d*?(?:(?:(?!00)[02468][048]|[13579][26])|(?:(?:[02468][048]|[13579][26])00))|[48]00|[48])(?=\D)` – ctwheels Oct 25 '17 at 19:11
  • @ctwheels - Not bad! If you change this part `(?:(?!00)[02468][048]|[13579][26])` to this `(?:0[48]|[13579][26]|[2468][048])` it will run twice as fast. –  Oct 25 '17 at 19:55
  • 1
    My bad, tot twice as fast, about %20 faster. `Regex1: ^(?:\d*?(?:(?:(?!00)[02468][048]|[13579][26])|(?:(?:[02468][048]|[13579][26])00))|[48]00|[48])$ Completed iterations: 500 / 500 ( x 1 ) Matches found per iteration: 2041 Elapsed Time: 2.75 s, 2753.28 ms, 2753282 µs Regex2: ^(?:\d*?(?:(?:0[48]|[13579][26]|[2468][048])|(?:(?:[02468][048]|[13579][26])00))|[48]00|[48])$ Completed iterations: 500 / 500 ( x 1 ) Matches found per iteration: 2041 Elapsed Time: 2.29 s, 2287.64 ms, 2287645 µs` –  Oct 25 '17 at 20:10
0

[0-9]{2} - this is month, right? Shouldn't it look like that: (0[0-9])|(1[0-2]) ??

And again [0-9]{2} for day: ([0-2][0-9])|(3[01]) [0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{4}:[0-9]{2}:[0-9]{2}+[0-9]+

I'm not sure about "time" part.

PS You have to be more specific, because I'm not sure if I understand.