正文

日期正则表达式2008-08-17 18:26:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/iamben250/37679.html

分享到:

Title Test Details Date With Slashes
Expression
^\d{1,2}\/\d{1,2}\/\d{4}$
Description
This regular expressions matches dates of the form XX/XX/YYYY where XX can be 1 or 2 digits long and YYYY is always 4 digits long.
Matches
4/1/2001 | 12/12/2001 | 55/5/3434
Non-Matches
1/1/01 | 12 Jan 01 | 1-1-2001

 

Expression
(^|\s|\()((([1-9]){1}|([0][1-9]){1}|([1][012]){1}){1}[\/-]((2[0-9]){1}|(3[01]){1}|([01][1-9]){1}|([1-9]){1}){1}[\/-](((19|20)([0-9][0-9]){1}|([0-9][0-9]){1})){1}(([\s|\)|:])|(^|\s|\()((([0-9]){1}|([0][1-9]){1}|([1][012]){1}){1}[\/-](([11-31]){1}|([01][1-9]){1}|([1-9]){1}){1}[\/-](((19|20)([0-9][0-9]){1}|([0-9][0-9]){1})){1}(([\s|\)|:|$|\>])){1}){1}){1}){1}
Description
Will match the following date formats: Preceded by a Space, Left-parentheses, or at the beginning of a line. Followed by a Space, Right-parentheses, or Colon(:), word boundary or End of line. Can have / or - as separator. Accepts 2 digit year 00-99 or 4 digit years 1900-2099 (can modify to accept any range)
Matches
01/01/2001 | 01-01-2001: | (1-1-01)
Non-Matches
13/1/2001 | 1-32-2001 | 1-1-1801

Expression
^(([1-9])|(0[1-9])|(1[0-2]))\/(([0-9])|([0-2][0-9])|(3[0-1]))\/(([0-9][0-9])|([1-2][0,9][0-9][0-9]))$
Description
Dates day: d or dd, <= 31, month: m or mm, <= 12, year: yy or yyyy >= 1900, <= 2099
Matches
01/01/2001 | 1/1/1999 | 10/20/2080
Non-Matches
13/01/2001 | 1/1/1800 | 10/32/2080

Expression
((0?[13578]|10|12)(-|\/)((0[0-9])|([12])([0-9]?)|(3[01]?))(-|\/)((\d{4})|(\d{2}))|(0?[2469]|11)(-|\/)((0[0-9])|([12])([0-9]?)|(3[0]?))(-|\/)((\d{4}|\d{2})))
Description
Regex used in .NET to validate a date. Matches the following formats mm/dd/yy, mm/dd/yyyy, mm-dd-yy, mm-dd-yyyy This covers days with 30 or 31 days but does not handle February, it is allowed 30 days.
Matches
1/31/2002 | 04-30-02 | 12-01/2002
Non-Matches
2/31/2002 | 13/0/02 | Jan 1, 2001

Expression
^\d{4}[\-\/\s]?((((0[13578])|(1[02]))[\-\/\s]?(([0-2][0-9])|(3[01])))|(((0[469])|(11))[\-\/\s]?(([0-2][0-9])|(30)))|(02[\-\/\s]?[0-2][0-9]))$
Description
- validates a yyyy-mm-dd, yyyy mm dd, or yyyy/mm/dd date - makes sure day is within valid range for the month - does NOT validate Feb. 29 on a leap year, only that Feb. CAN have 29 days
Matches
0001-12-31 | 9999 09 30 | 2002/03/03
Non-Matches
0001\02\30 | 9999.15.01 | 2002/3/3

Expression
((([0][1-9]|[12][\d])|[3][01])[-/]([0][13578]|[1][02])[-/][1-9]\d\d\d)|((([0][1-9]|[12][\d])|[3][0])[-/]([0][13456789]|[1][012])[-/][1-9]\d\d\d)|(([0][1-9]|[12][\d])[-/][0][2][-/][1-9]\d([02468][048]|[13579][26]))|(([0][1-9]|[12][0-8])[-/][0][2][-/][1-9]\d\d\d)
Description
Date validation in the dd/mm/yyyy format for years 1000+ (i.e 999 or 0999 not matching) and taking february leap years into account.
Matches
12/12/2003 | 29-02-2004 | 31-03-1980
Non-Matches
29/02/2003 | 31-04-2002 | 10-10-0999

Title Test Details mm/dd/yyyy Julian and Gregrian Datetime
Expression
(?#Calandar from January 1st 45 BC to December 31, 9999 in mm/dd/yyyy format) (?! (?:10(?<sep>[-./])(?:0?[5-9]|1[0-4])\k<sep>(?:1582))| #Missing days from 1582 (?:0?9(?<sep>[-./])(?:0?[3-9]|1[0-3])\k<sep>(?:1752)) #or Missing days from 1752 (?# both sets of missing days such not be in the same calendar so remove one or the other) ) (?n:^(?=\d) # the character at the beginning a the sring must be a digit ( (?<month> (0?[13578])|1[02]| #months with 31 days (0?[469]|11)(?!.31)| # months with 30 days 0?2 # February (?(.29) # if feb 29th check for valid leap year (?=.29. (?! #exclude these years from leap year pattern 000[04] #No year 0 and no leap year in year 4 | (?:(?:1[^0-6]|[2468][^048]|[3579][^26])00) (?# centurial years > 1500 not evenly divisible by 400 are not leap year) ) (?:(?:(?:\d\d) # century (?:[02468][048]|[13579][26]) #leap years (?!\x20BC))|(?:00(?:42|3[0369]|2[147]|1[258]|09)\x20BC)) )| # else if not Feb 29 (?!.3[01]) # and day not Feb 30 or 31 ) #end Leap year check ) #end of month check (?<sep>[-./]) # choose a date separator (?<day>0?[1-9]|[12]\d|3[01]) #days between 1-31 (?# The maximum number of days allowed for a month has already been checked for in the month check. If you made it this far the number of day is within the range for the given month) \k<sep> # Match the same date separator choosen before. (?!0000) # There is no year 0 (?<year>(?=(?:00(?:4[0-5]|[0-3]?\d)\x20BC)|(?:\d{4}(?:\z|(?:\x20\d))))\d{4}(?:\x20BC)? # a four digit year. Use leading zeros if needed ) (?(?=\x20\d)\x20|$))? # if there is a space followed by a digit check for time (?<time> ( # 12 hour format (0?[1-9]|1[012]) # hours (:[0-5]\d){0,2} # optional minutes and seconds (?i:\x20[AP]M) # required AM or PM )| # 24 hour format ( [01]\d|2[0-3]) #hours (:[0-5]\d){1,2}) #required minutes optional seconds ?$)
Description
Datetime for Julian and Gregorian Calenders Matchs dates from 0001 A.D. to 9999 A.D. Days and months are 1 or 2 digits Years are 4 digit with leading zeros if required. February is validate in all leap years Leap year rules for Julian and Gregorian calendars (http://scienceworld.wolfram.com/astronomy/LeapYear.html) Missing days for 1582 and 1752 are not matched. Though only one set should be applied to a calendar since they are caused by when the calendar was adopted Missing days (http://scienceworld.wolfram.com/astronomy/GregorianCalendar.html) Time can be either 12 or 24 hour format 12 hour format hh:MM:ss AM|PM minutes and seconds are optional 24 hour format hh:mm:ss seconds are optional, hours less than ten require leading zero Datetome format is a date, a space then a time.
Matches
12/25/0004 | 12/31/0001 BC 2:15 AM | 2-29-2004 09:00
Non-Matches
00/00/0000 | 2-29-2100 | 10/8/1582

Title Test Details Pattern Title
Expression
^((((([0-1]?\d)|(2[0-8]))\/((0?\d)|(1[0-2])))|(29\/((0?[1,3-9])|(1[0-2])))|(30\/((0?[1,3-9])|(1[0-2])))|(31\/((0?[13578])|(1[0-2]))))\/((19\d{2})|([2-9]\d{3}))|(29\/0?2\/(((([2468][048])|([3579][26]))00)|(((19)|([2-9]\d))(([2468]0)|([02468][48])|([13579][26]))))))\s(([01]?\d)|(2[0-3]))(:[0-5]?\d){2}$
Description
My definitive Date and Time pattern (dd/mm/yyyy hh:mm:ss). It recognizes all correct dates (&gt;1900) and time (even february). What can I say, I'm proud of it
Matches
29/2/2004 15:16:57 | 29/01/1900 18:17:15
Non-Matches
29/2/1900 14:13:11

 

阅读(2628) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册