Question

In: Computer Science

Define a regular expression that will correctly (and exactly) match a valid Gregorian calendar date of...

Define a regular expression that will correctly (and exactly) match a valid Gregorian calendar date of the form MM/DD/YYYY (with leading zeroes for single-digit months and days) (and ONLY dates in this format). • Months 1, 3, 5, 7, 8, 10, and 12 have 1–31 days • Months 4, 6, 9, and 11 have 1–30 days • Month 2 (February) has 1–28 days (ignore leap years) • Assume that the year falls into the range 1900–2099 HINT: You may find it helpful to define several patterns and combine them using the alternation operator.

Solutions

Expert Solution

Hi,

Here is the regular expression matching a valid Gregorian calendar date of the form MM/DD/YYYY.

I have combined the various regular expression using the alteration operator |

Please find the regular expression.

^(02(\/)29(\/)(2000|(19|2[0](0[48]|[2468][048]|[13579][26]))))$|^((02(\/)(0[1-9]|1[0-9]|2[0-8])(\/)(19|2[0])[0-9]{2}))$|^(((0[13578]|10|12)(\/)(0[1-9]|[12][0-9]|3[01])(\/)(19|2[0])[0-9]{2}))$|^(((0[469]|11)(\/)(0[1-9]|[12][0-9]|30)(\/)(19|2[0])[0-9]{2}))$

Explanation :

Step 1 :

^(02(\/)29(\/)(2000|(19|2[0](0[48]|[2468][048]|[13579][26]))))$

  • 2000 matches a set of leap years with a divider of 400 in a restricted range of 1900-2999
  • 19|2[0-9](0[48]|[2468][048]|[13579][26])) matches all white-list combinations of years which have a divider of 4 and don't have a divider of 100
  • -02-29 matches February 2nd

Step 2 :

^((02(\/)(0[1-9]|1[0-9]|2[0-8])(\/)(19|2[0])[0-9]{2}))$

Matching February 29th in leap years, we also need to match all other days of February (1 – 28) in all years

Step 3:

^(((0[13578]|10|12)(\/)(0[1-9]|[12][0-9]|3[01])(\/)(19|2[0])[0-9]{2}))$

The months January, March, May, July, August, October, and December should match for between 1 and 31 days

Step 4:

^(((0[469]|11)(\/)(0[1-9]|[12][0-9]|30)(\/)(19|2[0])[0-9]{2}))$

The months April, June, September, and November should match for between 1 and 30 days


Related Solutions

Question d) Write the regular expression statement that will match a string that starts with the...
Question d) Write the regular expression statement that will match a string that starts with the word "Warning" following by one space and then a number of any number of digits. Question e) Write the regular expression statement that will match a string that ends with the phrase "End of Report. Question f) Write the regular expression statement that will match a string that starts with the phrase "Begin Log:" then contains a collection of characters (other than newline) followed...
Define a regular expression in JAVA for the following An "Ent" is an at sign '@',...
Define a regular expression in JAVA for the following An "Ent" is an at sign '@', followed by one or more decimal digits '0' through '9' or uppercase letters 'R' through 'W', followed by an octothorpe sign '#'
Q1 Write a python regular expression to match a certain pattern of phone number.             ###...
Q1 Write a python regular expression to match a certain pattern of phone number.             ### Suppose we want to recognize phone numbers with or without hyphens. The ### regular expression you give should work for any number of groups of any (non- ### empty) size, separated by 1 hyphen. Each group is [0-9]+. ### Hint: Accept "5" but not "-6" ### FSM for TELEPHONE NUMBER IS: # state:1 --[0-9]--> state:2 # state:2 --[0-9]--> state:4 # state:2 --[\-]---> state:3 #...
Write a regular expressions that would match lines in text that contain word DATE: at the...
Write a regular expressions that would match lines in text that contain word DATE: at the beginning of a line followed by actual date in format YYYY-MM-DD. Space between colon ( : ) and date may or may not exist. In C, if you issue the following statement n << 2 where n is an integer, what will be value of n? In bash, if you define variable var = “date” and issue a statement echo `$var`, what output will...
Write a regular expressions that would match lines in text that contain word DATE: at the...
Write a regular expressions that would match lines in text that contain word DATE: at the beginning of a line followed by actual date in format YYYY-MM-DD. Space between colon ( : ) and date may or may not exist. In C, if you issue the following statement n << 2 where n is an integer, what will be value of n?
1. Define a regular expression for validating each of the following: a. A U.S. zip code,...
1. Define a regular expression for validating each of the following: a. A U.S. zip code, which must have five digits, followed by an optional hyphen and four digits. b. A telephone number in the format (aaa) nnn-nnnn, where aaa represents the area code and the ns represent digits. c. A date in the form of day-month-year, where the month is a three-letter code and the year is four digits. A hyphen must separate the day and month and year...
Regular expression translation [ac7]b[bc] Translation: Match any 1 character in a set that is either a,...
Regular expression translation [ac7]b[bc] Translation: Match any 1 character in a set that is either a, c, or 7, then a b, then any 1 character that is in a set that is either b or c. Match example: abc ^…$ \<the\> ^[A-Z]..$ ^[A-Z][ a-z]*3[0-5] [a-z]*\.$ ^ *[A-Z][a-z][a-z]$ ^[A-Za-z]*[^,][A-Za-z]*$ [[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3} .*\([0-9][.][0-9]\) [0-9]{3}-[0-9]{4}
If you would define a rule for creating a variable in C using regular expression, how...
If you would define a rule for creating a variable in C using regular expression, how would you write it? If you need to skip statements in for or while loops and go to the next iteration, what statement will you use? In respect to users and processes, how would you define Linux OS?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT