Like any other exam, this is a “solo effort” (collaboration between individual students is not permitted) and the general Student Academic Integrity Policy applies . External resources are to be used for reference purposes only; blatant copying of solutions found on-line (or from other students) could result in receiving a zero for the entire exam.
• Unless otherwise specified for the Regular Expression questions, you can assume that the expression is being applied to some arbitrary text file where the contents is not known in advance.
• Unless the question states that you should write a complete program, you should only write the expression and NOT a complete program.
• All references to “Perl Style” regular expressions simply means that the expression should be written in the form: m/match_pattern/ or s/match_pattern/substitution_pattern/
(depending on whichever is appropriate).
Note: Much of the text has been “locked” in this document, not as a “security measure”, but to prevent accidental editing or deletion of questions. The area beneath each question should be freely editable and feel free to add to remove space between questions as you need!
a) (2 pts) Which language(s) have regular expression evaluation integrated into the syntax of the language.
b) (2 pts) Which language(s) require the import of additional “regular expression” library, before regular expression searching/matching/substitution can be use.
For the following questions, which you have write a regular expression, ONLY the expression and nothing but the expression is needed. Complete “functions” are not necessary, nor should they be included as part of the answer. For convenience you may assume that any “new-line” makers are never “consumed” as part of a match and the can be safely ignored.
2. (2 pts) Show a regular expression that will match any a string of characters which contains any number of occurrences of the letter A (and only the letterA, with no other characters or spaces).
(EXAMPLE STRING: AAAAA).
3. (2 pts)Show a regular expression that will match any a string of characters which contains at least on occurrence of the letter A, if it is not either the first character or the last character of the string:
(EXAMPLE STRING: BCDABC)
4. (2 pts) Show a regular expression would match only a line with exactly 100 character?
5. (2 pts) Show a regular expression would match all words in a string that begin and end with the same letter? (EXAMPLE STRING: “algebra” matches but “amoebae” does not.)
6. (2 pts) Write a “Perl style” regular expression which would match any line containing adjacent repeated words. For example:
this is is a line (would match against the expression.)
this is a line (would NOT match against the expression.)
7. (2 pts)A file consists of nothing but a series of value pairs in the following form username=userid, where username can be any combination of letters and numbers and userid is and any numerical value. Write a “Perl style” regular expression which will swap the username and userid values, so username=userid becomes userid=username.
(EXAMPLE: abcd1=1234 would become 1234=abcd1)
8. (2 pts)Write a “Perl style” regular expression that would search/replace all occurrences of 10 consecutive digits in a string (only 10, no more no less; e.g., 1234567890) in a string with the exact same digits in the following format: 123-456-7890
Search String: Here are some numbers 123456780 which match.
Becomes: Here are some numbers 123-456-7890 which match.
Search String: Here are some numbers 1234454956780 which do not match.
Stays: Here are some numbers 1234454956780 which do not match.
9. (5 pts)Using regular expressions, write a complete Perl script that, when given input from a file as shown in the sample as shown below, displays ONLY the valid phone numbers found anywhere in the file. For this example, assume a phone number is valid only if it is in the following – rather unusual – form (where 9 represents any number):-999-9999
this is a test
My number: -555-1212
Not a phone number: -5555-1212
test 9999 test.
A phone number (-999-0000) exists on this line.
Alice Adventures in Wonderland.
For each of the following, use the alice.txt file which is attached to this “Assignment” in Blackboard. Though you must use “Regular Expressions”in some manner to arrive at the solution, you may use either Perl or Python for the “code portion” of the answer.
Show the regular expression (or complete code where applicable) to determine each of the following:
10. (2 pts)Show all of the words with 15 or more letters in the word.
11. (2 pts) Show the count of the number of occurrences of the word “Alice” in the file.
12. (2 pts)Show the line of the file containing the greatest number of occurrences of the word “Alice”.
13. (2 pts)List/show all of the URL’s (http: or https:) in the file.
14. (2 pts)Eliminate/remove/exclude all lines containing only asterisks from the file.
15. (2 pts)List/show all the phrases that appear between single quotes. Note that apostrophes and single quotes use the same character (‘)
The exam document is
and the alice.txt
is used in several questions.
Please note that most questions only require a single (one line) “Regular Expression”. Questions which ask for a “complete program” should only require a few lines; nothing longer (or significantly different) than that which is illustrated in this example from class.