UK Date Regular Expression

I am trying to create a regex that checks the date format in the UK. I have the following:

(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d 

This works great for validation: 09/12/2011 . But if the date is: 9/12/2011, it will not be checked correctly. Is there a regular expression that allows me to use one number and two numbers for the day section? For example, " 09 " and " 9 ".

+4
source share
7 answers

Just make leading 0 optional:

 (0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)\d\d 

You will need an additional verification step, although this regular expression will certainly not check for invalid dates, such as 31-02-2000 , etc. Although this can be done in regex, it is not recommended because it is much easier to do it programmatically and this regex will be monstrous. Here is an example of date validation (which uses the mmddyyyy format) to show what I mean.

+8
source

My preference is for a combination of a simple regular expression (\d{1,2})[-/.](\d{1,2})[-/.](\d{4}) with some code that confirms that this is really the right date. You will need to have this code anyway if you do not want to make a monstrous regular expression that rejects "29-02-2011" but not "29-02-2008".

Anyway, here is a breakdown of this regex so you can see what happens:

  • \d{1,2} : this part corresponds to one or two ( {1,2} ) digits ( \d ), making up the daily part of the date.
  • [-/.] : this corresponds to one of the characters inside the brackets, that is, a . , a / or - .
  • \d{1,2} : again, this corresponds to one or two digits from the month.
  • [-/.] : another delimiter ...
  • \d{4} : this corresponds to four digits ( {4} ) for part of the year.

Note that in parentheses is the portion of the day, month, and year of the regular expression. This is done to create groups. Each of these three parts will be recorded in a group that you can get from the match. Groups are identified by a number, starting from 1, from left to right. This means that the day will be group 1, group of months 2 and group of 3 years. There is also group 0, which always contains all the text matched.

You can use groups to perform the second part of the check and reject invalid dates like "30-02-2011", "31-4-2011" or "32-13-2011".

If you want to reject input that uses two different delimiters, for example, "12/31/2011", you can use a slightly more advanced function called backreferences:

 (\d{1,2})([-/.])(\d{1,2})\2(\d{4}) 

Notice that now I have placed the first delimiter inside the group. This changes the month to group 3, and the year to group 4. The separator is mapped to group 2. The exception is \2 part between month and year. It corresponds to all that corresponded to the second previous group. If you release two groups from the backlink, you will end up in group 2, the delimiter. If this group matches a . , backreference matching only a . ; if it matches a - , the backlink will match only - ; etc.

+3
source

What is the UK date format?

Officially, it is 2011-02-21 today, see BS EN 28601 / ISO 8601.

On the Internet, you must use the format defined in RFC 3339.

+1
source

Yes. {n,m} is a quantifier that says "at least n elements, max m elements." Thus, you can write \d{1,2} (matches 1 or 2 digits). Completion Date: \d{1,2}/\d{1,2}/\d{4}

Alternative: Make the leading zero optional:

 0?\d/0?\d/\d{4} 

A question mark indicates that the item before the question mark is optional.

0
source

The correct way to check the day is also to prohibit numbers [4-9]. .

Something like 0[0-9]|[12][0-9]|3[01]|[^0-9][0-9]|^[0-9]

0
source

Use this code, I check everything for a date .: -

 import java.util.regex.Matcher; import java.util.regex.Pattern; public class FinalDateValidator { private Pattern pattern; private Matcher matcher; public boolean isValidDate(final String date) { Pattern pattern; Matcher matcher; final String DATE_PATTERN = "([0-9]{4})/(0?[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01]|[1-9])"; pattern = Pattern.compile(DATE_PATTERN); matcher = pattern.matcher(date); if (matcher.matches()) { matcher.reset(); if (matcher.find()) { int year = Integer.parseInt(matcher.group(1)); String month = matcher.group(2); String day = matcher.group(3); System.out.println("__________________________________________________"); System.out.println("year : "+year +" month : "+month +" day : "+day); if (day.equals("31") && (month.equals("4") || month.equals("6") || month.equals("9") || month.equals("11") || month.equals("04") || month.equals("06") || month .equals("09"))) { return false; // only 1,3,5,7,8,10,12 has 31 days } else if (month.equals("2") || month.equals("02")) { // leap year if (year % 4 == 0) { if (day.equals("30") || day.equals("31")) { return false; } else { return true; } } else { if (day.equals("29") || day.equals("30") || day.equals("31")) { return false; } else { return true; } } } else { return true; } } else { return false; } } else { return false; } } public static void main(String argsp[]){ FinalDateValidator vs = new FinalDateValidator(); System.out.println("1: 1910/12/10---"+vs.isValidDate("1910/12/10")); System.out.println("2: 2010/2/29---"+vs.isValidDate("2010/02/29")); System.out.println("3: 2011/2/29---"+vs.isValidDate("2011/02/29")); System.out.println("3: 2011/2/30---"+vs.isValidDate("2011/02/30")); System.out.println("3: 2011/2/31---"+vs.isValidDate("2011/02/31")); System.out.println("4: 2010/08/31---"+vs.isValidDate("2010/08/31")); System.out.println("5: 2010/3/10---"+vs.isValidDate("2010/03/10")); System.out.println("6: 2010/03/33---"+vs.isValidDate("2010/03/33")); System.out.println("7: 2010/03/09---"+vs.isValidDate("2010/03/09")); System.out.println("8: 2010/03/9---"+vs.isValidDate("2010/03/9")); System.out.println("9: 1910/12/00---"+vs.isValidDate("1910/12/00")); System.out.println("10: 2010/02/01---"+vs.isValidDate("2010/02/01")); System.out.println("11: 2011/2/03---"+vs.isValidDate("2011/02/03")); System.out.println("12: 2010/08/31---"+vs.isValidDate("2010/08/31")); System.out.println("13: 2010/03/39---"+vs.isValidDate("2010/03/39")); System.out.println("14: 201011/03/31---"+vs.isValidDate("201011/03/31")); System.out.println("15: 2010/032/09---"+vs.isValidDate("2010/032/09")); System.out.println("16: 2010/03/922---"+vs.isValidDate("2010/03/922")); } } 

Enjoy ...

0
source

I faced similar requirements. Here is the full regex along with the Leap Year check. Format: dd / MM / yyyy

(3 [01] | [12] \ d | 0 [1-9]) / (0 [13578] | 10 | 12) / ((?! 0000) \ d {4}) | (30 | [12] \ d | 0 [1-9]) / (0 [469] | 11) / ((?! 0000) \ d {4}) | (2 [0-8] | [01] \ d | 0 [1-9]) / (02) / ((?! 0000) \ d {4}) | 29 / (02) / (1600 | 2000 | 2400 | 2800 | 00) | 29 / (02) / (\ d \ d) (0 [48] | [2468] [048] | [13579] [26])

It can be easily changed in US format or other EU formats.

0
source

Source: https://habr.com/ru/post/1338400/


All Articles