Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. require 5.010;
  2. my $sep = qr{ [/.-] }x; #allowed separators
  3. my $any_century = qr/ 1[6-9] | [2-9][0-9] /x; #match the century
  4. my $any_decade = qr/ [0-9]{2} /x; #match any decade or 2 digit year
  5. my $any_year = qr/ $any_century? $any_decade /x; #match a 2 or 4 digit year
  6.  
  7. #match the 1st through 28th for any month of any year
  8. my $start_of_month = qr/
  9. (?: #match
  10. 0?[1-9] | #Jan - Sep or
  11. 1[0-2] #Oct - Dec
  12. )
  13. ($sep) #the separator
  14. (?:
  15. 0?[1-9] | # 1st - 9th or
  16. 1[0-9] | #10th - 19th or
  17. 2[0-8] #20th - 28th
  18. )
  19. \g{-1} #and the separator again
  20. /x;
  21.  
  22. #match 28th - 31st for any month but Feb for any year
  23. my $end_of_month = qr/
  24. (?:
  25. (?: 0?[13578] | 1[02] ) #match Jan, Mar, May, Jul, Aug, Oct, Dec
  26. ($sep) #the separator
  27. 31 #the 31st
  28. \g{-1} #and the separator again
  29. | #or
  30. (?: 0?[13-9] | 1[0-2] ) #match all months but Feb
  31. ($sep) #the separator
  32. (?:29|30) #the 29th or the 30th
  33. \g{-1} #and the separator again
  34. )
  35. /x;
  36.  
  37. #match any non-leap year date and the first part of Feb in leap years
  38. my $non_leap_year = qr/ (?: $start_of_month | $end_of_month ) $any_year/x;
  39.  
  40. #match 29th of Feb in leap years
  41. #BUG: 00 is treated as a non leap year
  42. #even though 2000, 2400, etc are leap years
  43. my $feb_in_leap = qr/
  44. 0?2 #match Feb
  45. ($sep) #the separtor
  46. 29 #the 29th
  47. \g{-1} #the separator again
  48. (?:
  49. $any_century? #any century
  50. (?: #and decades divisible by 4 but not 100
  51. 0[48] |
  52. [2468][048] |
  53. [13579][26]
  54. )
  55. |
  56. (?: #or match centuries that are divisible by 4
  57. 16 |
  58. [2468][048] |
  59. [3579][26]
  60. )
  61. 00
  62. )
  63. /x;
  64.  
  65. my $any_date = qr/$non_leap_year|$feb_in_leap/;
  66. my $only_date = qr/^$any_date$/;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement