IT3110 @ utahtech
Design and use regular expressions
In this assignment you will form various regular expressions. We will be working with the wordlist found on most linux installations. Mine is found at /usr/share/dict/words
. If you do not have this file you can install it with a sudo apt-get install wamerican
. All of the regular expressions can be tested by doing the following command: egrep "EXPRESSION" /usr/share/dict/words
. There may be more than one answer for a particular question. You should NOT use any other pipes. All results should be contained within a single regular expression unless otherwise indicated. Each question will be coded into a bash function. I.e. the first regular expression would be put in a function named one
.
egrep “^foo.*s$” /usr/share/dict/words
I would look at it to see if I am getting the desired results. Once I am sure that I am getting the desired results I would put that in my function like this:
function one () {
egrep "^foo.*s$" /usr/share/dict/words
}
You may create a main
function to call all your other ones, but you should call main as explained in previous assignments.
Continue for all the questions below:
Try this expression: ^(.).\1$
. The ^ matches the beginning of the line. The . means any character. The . within ( ) means capture this as a sub‐pattern. The next . matches any character. The \1 refers to the pattern captured in the ( ). The $ means match the end of the line. Now find all five letter words that start and end with the same two letters, except that the last 2 letters are reversed (a palindrome). radar
is an example.
Do the same thing as number 8 but find all words that start and end with the same 3 letters (the last 3 letters would be in reverse order from the first 3). The word could be 6 or 7 characters long. Hint: I only found 2 words in the list that met this criteria.
Find all words that have an aeiou (in order). Each vowel appears only once in the word. I only found 3.
Display all words that have 3 consecutive double-letter pairs (like “bookkeeper” has oo, kk, ee)?
Find all lowecase [a-z] words that have an apostrophe, but do not end in the letter ‘s’.
Find all words that consist of only 2 lowercase letters. (The word can only be 2 characters long)
Validate an email. The function fourteen
should receive an string. If it is valid email print out ‘email is valid, otherwise print out
email is invalid`. (email should be replaced with the actual email).
Validate an ip. The function fifteen
should receive an string. If it is valid ip print out ‘ip is valid, otherwise print out
ip is invalid`. (ip should be replaced with the actual ip).
sixteen
should receive an string. If it is valid print out ‘dnumber is valid, otherwise print out
dnumber is invalid. (dnumber should be replaced with the actual dnumber.) (A dnumber always starts with
d` and is followed by 8 digits).To see if you are doing it correctly you can look below to see if you are getting similar counts for functions 1-13. You could pipe all your function calls in main to the wc
command.
190 190 1943
28 28 249
63 63 598
17 17 192
912 912 14593
25 25 189
39 39 344
15 15 90
2 2 15
3 3 33
5 5 62
31 31 212
112 112 336
This should be uploaded as week5/assignment.sh
to your github repo.