0

Similar to How to use REGEX patterns to return day of the week if a date is entered? except I'm only looking for Regex answers not "any other clever method".

What is is simplest regex to match a particular day of the week, e.g. Thursday from a date string formatted like 2017-05-04T10:14:07Z.

This is what I have as a starting point.

2017-05-(04|11|18|25)T.*$

Is there a way to achieve a solution without too many pipes and covering all possible years or at least the last decade?

Andrew Downes
  • 1,068
  • 8
  • 20
  • 4
    In general I don't think that just a regex alone can give you the day of the week from your timestamp. Regex is good for extracting information from something, but it has no other knowledge of its own. – Tim Biegeleisen May 04 '17 at 15:49
  • It's definitely *possible*, at least for a fixed time period as shown in the example in my question. I'm wondering if there are any patterns or tricks to avoid having to list every possible combination though. – Andrew Downes May 04 '17 at 15:52
  • *Thursday from a date string formatted...* Oh no sir. You heard wrong about Regular Expressions. – revo May 04 '17 at 15:52
  • A more typical use of a regex would be to extract the date portion of the string, and then using a date library in a language such as Java, C#, Python, etc., get the actual day of the week. – Tim Biegeleisen May 04 '17 at 15:53
  • It may be possible, but you'd have to figure out a load of math, make sure you compensate for leap years, then figure out how to match that to textual numbers efficiently enough to not slow down the whole regex. If you can do it, then good on you! – Whothehellisthat May 04 '17 at 19:02
  • What language is it you are using? – Andreas May 04 '17 at 20:08

1 Answers1

0

First and foremost, Thursday can not be extracted from a date string formatted like 2017-05-04T10:14:07Z, as "Thursday" never appears in the string. The best you can capture is the 2 digit day number (the "04").

You CAN get the day number (\d+-\d{1,2}-(\d{1,2})T.*?Z), However regex can't verify the correctness of the day. (for example, for a random year, can you tell me if Feb 28 is valid without listing every single instance?) So ONLY DO THIS IF YOU ACCEPT DAY MAY NOT BE VALID (or source will always be right)

Tezra
  • 8,463
  • 3
  • 31
  • 68