Several programming languages in particular scripting langu

Several programming languages - in particular scripting languages - provide support for regular expressions. Regular expressions generalize strings. They define sets of strings, rather than individual items. Typically, they are used to match \"patterns\" in given strings. Most languages use the \"/\" character to delimit regular expressions. The syntax is similar to the one of string literals. Those are delimited by double, or single quotes. For example,

defines a pattern that matches all two-letter strings (or substrings) that begin with an uppercase letter A-Z, followed by a lowercase letter a-z. Some positive matches would be \"An\" or \"Of\", but also \"Th\" and \"En\" inside \"The End\". Every regular expression can be translated into a finite state machine. Therefore, they provide an alternate way of defining the grammar of Regular Languages (Type 3 languages in Chomsky\'s hierarchy).

Your task is to derive a regular expression for each pattern below (a. - c.). You can have your \"regex\" tested on-line, by a JavaScript program at www.regular-expressions.info (the web site has useful information for regular expressions in various programming languages). Such expressions are widely used in e-commerce applications to validate form data before clients submit them to a server.

a) Extended (9-digit) ZIP codes; e.g., 30533-1015.

b) Date strings in short form, either 10/31/2015 or 10/31/15.

c) UGA student email ids are of the form: FMLast1234@uga.edu.
Note, that some people do not have a middle initial (\"M\"). You only have to come up with a pattern for the id itself (e.g., \"FMLast1234\"); the \"@uga.edu\" part has to match exactly.

Solution

a) ^[0-9]{5}(-)[0-9]{4}$
[Explanation:
^ asserts position at start of the string
Match a single character present in the list below [0-9]{5}
{5} Quantifier — Matches exactly 5 times
0-9 a single character in the range between 0 (ASCII 48) and 9 (ASCII 57) (case sensitive)
1st Capturing Group (-)
- matches the character - literally (case sensitive)
Match a single character present in the list below [0-9]{4}
{4} Quantifier — Matches exactly 4 times
0-9 a single character in the range between 0 (ASCII 48) and 9 (ASCII 57) (case sensitive)
$ asserts position at the end of the string
]

b) ^([0]{1}[1-9]{1})|([1]{1}[0-2]{1})(\\/)(((2)([0-9]{1}))|((3)([0-1]{1})|([0][1-9]{1})|([1][1-9]{1})))(\\/)(([0-9]{4})|([0-9]{2}))$
[Explanation:
1st Alternative ^([0]{1}[1-9]{1})
^ asserts position at start of the string
1st Capturing Group ([0]{1}[1-9]{1})
Match a single character present in the list below [0]{1}
{1} Quantifier — Matches exactly one time (meaningless quantifier)
0 matches the character 0 literally (case sensitive)
Match a single character present in the list below [1-9]{1}
{1} Quantifier — Matches exactly one time (meaningless quantifier)
1-9 a single character in the range between 1 (ASCII 49) and 9 (ASCII 57) (case sensitive)
2nd Alternative ([1]{1}[0-2]{1})(\\/)(((2)([0-9]{1}))|((3)([0-1]{1})|([0][1-9]{1})|([1][1-9]{1})))(\\/)(([0-9]{4})|([0-9]{2}))$
2nd Capturing Group ([1]{1}[0-2]{1})
Match a single character present in the list below [1]{1}
{1} Quantifier — Matches exactly one time (meaningless quantifier)
1 matches the character 1 literally (case sensitive)
Match a single character present in the list below [0-2]{1}
{1} Quantifier — Matches exactly one time (meaningless quantifier)
0-2 a single character in the range between 0 (ASCII 48) and 2 (ASCII 50) (case sensitive)
3rd Capturing Group (\\/)
\\/ matches the character / literally (case sensitive)
4th Capturing Group (((2)([0-9]{1}))|((3)([0-1]{1})|([0][1-9]{1})|([1][1-9]{1})))
1st Alternative ((2)([0-9]{1}))
5th Capturing Group ((2)([0-9]{1}))
6th Capturing Group (2)
2 matches the character 2 literally (case sensitive)
7th Capturing Group ([0-9]{1})
Match a single character present in the list below [0-9]{1}
2nd Alternative ((3)([0-1]{1})|([0][1-9]{1})|([1][1-9]{1}))
8th Capturing Group ((3)([0-1]{1})|([0][1-9]{1})|([1][1-9]{1}))
1st Alternative (3)([0-1]{1})
9th Capturing Group (3)
3 matches the character 3 literally (case sensitive)
10th Capturing Group ([0-1]{1})
2nd Alternative ([0][1-9]{1})
11th Capturing Group ([0][1-9]{1})
3rd Alternative ([1][1-9]{1})
13th Capturing Group (\\/)
\\/ matches the character / literally (case sensitive)
14th Capturing Group (([0-9]{4})|([0-9]{2}))
1st Alternative ([0-9]{4})
15th Capturing Group ([0-9]{4})
Match a single character present in the list below [0-9]{4}
2nd Alternative ([0-9]{2})
16th Capturing Group ([0-9]{2})
Match a single character present in the list below [0-9]{2}
$ asserts position at the end of the string
]


c) ^([A-Z]{1})((?:|([A-Z])){1})([A-Z][a-z]{3})([0-9]{4})(@uga\\.edu)$
[Explanation:
^ asserts position at start of the string
1st Capturing Group ([A-Z]{1})
Match a single character present in the list below [A-Z]{1}
{1} Quantifier — Matches exactly one time (meaningless quantifier)
A-Z a single character in the range between A (ASCII 65) and Z (ASCII 90) (case sensitive)
2nd Capturing Group ((?:|([A-Z])){1})
Non-capturing group (?:|([A-Z])){1}
{1} Quantifier — Matches exactly one time (meaningless quantifier)
1st Alternative — null, matches any position
2nd Alternative ([A-Z])
3rd Capturing Group ([A-Z])
Match a single character present in the list below [A-Z]
4th Capturing Group ([A-Z][a-z]{3})
Match a single character present in the list below [A-Z]
A-Z a single character in the range between A (ASCII 65) and Z (ASCII 90) (case sensitive)
Match a single character present in the list below [a-z]{3}
{3} Quantifier — Matches exactly 3 times
a-z a single character in the range between a (ASCII 97) and z (ASCII 122) (case sensitive)
5th Capturing Group ([0-9]{4})
Match a single character present in the list below [0-9]{4}
{4} Quantifier — Matches exactly 4 times
0-9 a single character in the range between 0 (ASCII 48) and 9 (ASCII 57) (case sensitive)
6th Capturing Group (@uga\\.edu)
@uga matches the characters @uga literally (case sensitive)
\\. matches the character . literally (case sensitive)
edu matches the characters edu literally (case sensitive)
$ asserts position at the end of the string  
]

Several programming languages - in particular scripting languages - provide support for regular expressions. Regular expressions generalize strings. They define
Several programming languages - in particular scripting languages - provide support for regular expressions. Regular expressions generalize strings. They define

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site