Regular expression question in Linux (Only write the regular expression. Do not
ID: 3891002 • Letter: R
Question
Regular expression question in Linux (Only write the regular expression. Do not include the egrep command and do not enclose it in single or double quotes.):
1. Write a regular expression that you could use with egrep that will match all sentences that contain exactly four words separated by a space with a punctuation mark at the end. Assume that the first word of a sentence is capitalized. (Some lines may contain more than one sentence.) Use the :alpha:, :upper: and :punct: classes where appropriate.
2. Write a regular expression that you could use with egrep that will match all lines in which the letters (upper or lower case) a, b, c, and d appear in that order (not necessarily consecutively). This should, for instance, find “a bat can die”.
3. Write a regular expression that you could use with egrep that will match lines in the names.txt file that have cities whose names are multiple (i.e. 2 or more) words.
Explanation / Answer
1) [A-Z][a-z]* [a-z]* [a-z]* [a-z]*.
[A-Z] - Sentence should start with Upper case letter
[a-z]* any combination of smaller case letters(* represents multiplse occurences)
- followed by space b/w words
2)[^bBcCdD]*[a|A][^cCdD]*[b|B][^dD]*[c|C].*[d|D]
[^bBcCdD] - any character other than bBcCdD
|- or condition
3).* .*
Any character can occur any number of times followed by space and any character can occur any number of times