Regex to validate date formats dd/mm/YYYY, dd-mm-YYYY, dd.mm.YYYY, dd mmm YYYY, dd-mmm-YYYY, dd/mmm/YYYY, dd.mmm.YYYY with Leap Year Support
The regex you pasted does not validate leap years correctly, but there is one that does in the same post.
I modified it to take dd/mm/yyyy
, dd-mm-yyyy
or dd.mm.yyyy
.
^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$
I tested it a bit in the link Arun provided in his answer and also here and it seems to work.
Edit February 14th 2019: I've removed a comma that was in the regex which allowed dates like 29-0,-11
Regex to match date formats DD-MM-YYYY and DD/MM/YYYY
This pattern will work for your conditon. Strictly not allowed month to date and vice versa.
^([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2})$
This pattern will match these formats: (Date, Month, Year)
25.04.2017
02.04.2017
2.4.2017
25/04/2017
5/12/2017
15/2/2017
25-04-2017
6-10-2017
16-5-2017
Regular expression to match a date range
Wouldn't it be easier to parse the strings to DateTime
s and comparing those?
RegEx for matching dates (Month Day, Year OR m/d/yy)
Your expression seems to work fine, both of them. If you wish to capture anything before or after your target output, you can simply add two boundaries in the left and right, which would do that for you. For example, please look at this test:
(.*)(((1[0-2]|0?[1-9])\/(3[01]|[12][0-9]|0?[1-9])\/(?:[0-9]{2})?[0-9]{2})|((Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4}))(.*)
where you can for instance add two groups similar to (.*)
and wrap your original expression in one group which will do so.
RegEx Descriptive Graph
The graph visualize how your expression works and you might want to test other expressions in this link:
C# Test
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(.*)(((1[0-2]|0?[1-9])\/(3[01]|[12][0-9]|0?[1-9])\/(?:[0-9]{2})?[0-9]{2})|((Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4}))(.*)";
string input = @"Paid on Jan 1, 2019 And anything else that you wish to have after
Paid on 1/1/2019 And anything else that you wish to have after";
RegexOptions options = RegexOptions.Multiline;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
JavaScript Demo
This JavaScript demo shows that your expression works:
const regex = /(.*)(((1[0-2]|0?[1-9])\/(3[01]|[12][0-9]|0?[1-9])\/(?:[0-9]{2})?[0-9]{2})|((Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4}))(.*)/gm;const str = `Paid on Jan 1, 2019 And anything else that you wish to have afterPaid on 1/1/2019 And anything else that you wish to have after`;const subst = `\nGroup 1: $1 \nGroup 2: $2 \nGroup 3: $3 \nGroup 4: $4 `;
// The substituted value will be contained in the result variableconst result = str.replace(regex, subst);
console.log('Substitution result: ', result);
Regular Expression to match valid dates
This is not an appropriate use of regular expressions. You'd be better off using
[0-9]{2}/[0-9]{2}/[0-9]{4}
and then checking ranges in a higher-level language.
Regex to find a date BEFORE a word
You could use a positive lookahead (?=\h+More\b)
at the end of your date like pattern to assert what follows is 1+ times a horizontal whitespace char followed by Word
and a word boundary.
(?:([0-9]+)/([0-9]+)/([0-9]+)|((0?[1-9]|1[0-2])-(0?[1-9]|[12]\d|3[01])-(\d{4}|\d{2}))|\w+\s\d{2},\s\d{4}|(?i)\b(Jan(?:uary|.)?|Feb(?:ruary|.)?|Mar(?:ch|.)?|Apr(?:il|.)?|May|Jun(?:e|.)?|Jul(?:y|.)?|Aug(?:ust|.)?|Sep(?:tember|.)?|Oct(?:ober|.)?|Nov(?:ember|.)?|Dec(?:ember|.)?)( ,?[ ]|-(?:0?[1-9]|[1-2][0-9]|3[01])-)(\d{4}))(?=\h+More\b)
Regex demo
If the word can be on a newline you could change \h
to \s
Regex demo
Regex to match date string greater than certain date
Since there is not so many dates after 20190605
, maybe this simple expression would work:
\/tmp\/(2019060[6-9]|201906[1-3][0-9]|20190[7-9][0-9]{2})
which is simple to modify, if a date is missing.
Demo
RegEx Circuit
jex.im visualizes regular expressions:
Related Topics
Difference Between Attr_Accessor and Attr_Accessible
Where and How Is the _ (Underscore) Variable Specified
When to Use Symbols Instead of Strings in Ruby
How to Implement an Abstract Class in Ruby
How to Extract Url Parameters from a Url With Ruby or Rails
Rails 3: Alias_Method_Chain Still Used
Ruby 1.9: How to Properly Upcase & Downcase Multibyte Strings
Open an Io Stream from a Local File or Url
How to Avoid "Cannot Load Such File - Utils/Popen" from Homebrew on Osx
How to Track System-Specific Config Files in a Repo/Project
Your Ruby Version Is 2.0.0, But Your Gemfile Specified 2.1.0
How to Split (Chunk) a Ruby Array into Parts of X Elements
Rails Keeps Telling Me That It's Not Currently Installed
Ruby'S File.Open and the Need For F.Close
Difference Between '..' (Double-Dot) and '...' (Triple-Dot) in Range Generation