Skip to content

Instantly share code, notes, and snippets.

@Zegnat
Created September 25, 2011 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zegnat/1240601 to your computer and use it in GitHub Desktop.
Save Zegnat/1240601 to your computer and use it in GitHub Desktop.
Regular expression for ISO 8601 datestamps
^
(\+|-)? # Extended year?
(\d{4,}) # Year
(
(
-? # Optional separator
(0[1-9]|1[012]) # Month, 01 to 12
(?(?<=-\d\d)-) # Require another separator if we used one before
(0[1-9]|[12][0-9]|3[01]) # Day, 01 to 31
)|(
-? # Optional separator
(00[1-9]|0[1-9]\d|[12]\d\d|3[0-5]\d|36[0-6]) # Ordinal day, 001 to 366
)
)
-(.*) # Required separator and slug
(\.[^.]+) # Required file extension
$
# Ruby does not do the conditional capturing group.
^
(\+|-)? # Extended year?
(\d{4,}) # Year
(
(
(
-(0[1-9]|1[012])- # Month, 01 to 12, with separators
|
(0[1-9]|1[012]) # Month, 01 to 12, without separators
)
(0[1-9]|[12][0-9]|3[01]) # Day, 01 to 31
)|(
-? # Optional separator
(00[1-9]|0[1-9]\d|[12]\d\d|3[0-5]\d|36[0-6]) # Ordinal day, 001 to 366
)
)
-(.*) # Required separator and slug
(\.[^.]+) # Required file extension
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment