In: Computer Science
QUESTION:
Write regular expressions after the ► to validate the following inputs:
a) Product codes that have one or more letters followed by one or more digits.
►
b) Nonnegative integers between 1,000 and 9,999, including the comma.
►
c) Canadian zip code containing upper case characters only.
►
d) License plate numbers that have the form:
Letter
Digit
Letter
Hypen (-)
Digit
Letter
Digit
►
Regular expressions in different laguages are similar expect the minor syntaxes.
a) Product codes that have one or more letters followed by one or more digits.
► "\^[a-zA-Z][0-9]\" :
Explanation: The product code should be like first one or more letters and followed by one or more numbers. First must be letter so, We have written a-z and A-Z letters can be smaller or capitals. then followed by numbers 0-9.
b) Nonnegative integers between 1,000 and 9,999, including the comma.
►"\^[123456789][,][0-9][0-9][0-9]\"
Explanation: Here We cannot give directly as 1000-9999 in square brackets. We can only give range 0-9, so with these the first digit should be 1 - 9 so we have taken this. then followed by a , then next three digits can be any between 0-9.
c) Canadian zip code containing upper case characters only.
►"\^[0-9A-Z]{2,}\"
Explanation: Canadian Zip code may consits numbers. And all letters must be capital so. We have taken 0-9 for any number and A-Z for capital letters. And I am assuming the postal code at least has 2 characters of length so you can change this accordingly.
d) License plate numbers that have the form:
Letter
Digit
Letter
Hypen (-)
Digit
Letter
Digit
►"\^[a-zA-Z][0-9][a-zA-Z][-][0-9][a-zA-Z][0-9]\"
Explanation: As said above a-z and A-Z in square brackets indicates letter and a 0-9 indicates a digit.