ITU – UNIX Operating System Homework Assignment #6 Answer the following question
ID: 3627578 • Letter: I
Question
ITU – UNIX Operating SystemHomework Assignment #6
Answer the following questions about regular expressions. You may test your answer in UNIX by using:
echo 'testpattern' | grep 'regexp'
For safety, put both testpattern and regexp inside single quotes
Given the following regular expressions, identify those patterns that will be matched (may be more than one). For those that match, identify which characters are matched, or explain your reasoning.
1) Regular expression: [ABC]
A
AB
ADBC
D
2) Regular expression: [^ABC]
A
AB
ADBC
D
3) Regular expression: [A-G]
A
AB
MBC
D
4) Regular expression: ^[ABC][AB]$
A
AB
ADBC
D
5) Regular expression: bc.*
aaabbbcccddd
aaaabcsssss
aaaaabc
aabbss
6) Regular expression: bc*.
aaabbbcccddd
aaaabcsssss
aaaaabc
aabbss
7) Regular expression: [^$]$
$$$$$$aaaaaa
bcdef$
$
abc
8) Regular expression: ^[^$]$
$$$$$$aaaaaa
bcdef$
$
abc
9) Regular expression: [^$]$
$$$$$$aaaaaa
bcdef$
$
abc
10) Regular expression: ^$[0-9][0-9]$
$$$$$$
$10
abc$
$35f
11) Regular expression: ^$[0-9]*[^0-9]$
$$$$$$
$10
abc$
$35f
12) Write a regular expression that matches a line with exactly three characters
13) Write a regular expression that matches a line with at least three characters
14) Write a regular expression that matches a date in the usual format: mm/dd/yy
For example, it should match 01/20/99 or 11/08/89 but not 13/41/99
15) Write a regular expression that determines the employees who make six digit salaries in the file emp.lst
Explanation / Answer
1. A- This is the only match because it is one character in the set [A,B,C] 2. D- This is the only match because it is one character that is NOT A, B, or C 3. A,D - These match because they are one character between A and G, inclusive 4. A, AB - These match because they must start with a character that is A,B, or C due to ^[ABC]. They also must end with a character that is A or B due to [AB]$. Both A and AB match. 5. None - There are no matches because none of them start with bc. There are substrings that match, however, but the question was unclear. For a match to occur, a pattern would start with bc and then contain any string afterward. 6. None - There are no matches because none of them start with b. There are substrings that match, however, but the question was unclear. For a match to occur, a pattern would start with b and then contain any number of c's, then contain a single character (any one). 7. $$$$$$aaaaaa, abc - These match because their last character is not "$". The character denotes that $ should be taken literally, and there's a ^ in front, so it means not. $ is at the end, so it means the last character can be anything but "$". 8. abc - This matches because it both starts and ends with a character that is not "$". [^$] denotes any character except $. ^ before says it must start with that and $ at the end means it has to end with that. abc is the only one that works. 9. Same question as #7 10. $10 - This matches because it starts with "$" and ends with two numbers. ^$ means start with a literal "$" character. And [0-9][0-9]$ means it must end with two characters that are between 0 and 9, inclusive. "$10" is the only one that works. 11. $$$$$$, $35f - these match because they have to start with "$" due to ^$. [0-9]* means that any number (even 0) of number characters between 0 and 9 can follow. Then it must end with a character that is not a number, due to [^0-9]$. 12. "..." 13. "....*" 14. "(0[1-9]|1[0-2])/(0[1-9]|[1-2][0-9]|3)/([0-9][0-9])" 15. "$[0-9][0-9][0-9],[0-9][0-9][0-9]" Hope this helps...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.