Grep and Regular Expressions - Replace

Practical Unix Lecture 10 of 24

 

< Previous
Next >
 

Most programming languages let you use regular expressions to do replacement. To refer to stuff you matched, you can refer to the number of the group.

If I opened Notepad++ and did a regex find for "(.*) foo bar" with a replace of "foo \1 bar", and there was a line like "baz foo bar", then Notepad++ would replace "baz foo bar" with "foo baz bar". Note that you can only refer to groups that you put inside parentheses.

You can ignore a group by starting it with (:. For instance, in the regex "(?:.*) (.*)", \1 refers to the second group.

You can refer to a group within the regular expression. Ie, "([ab])\1" matches aa or bb but not ab.


Explain why you need to use a r' string instead of a regular " string for python regular expressions. What would happen if you put a normal regular expression in a " string?

Open something that has regular expression find and replace: MS Word, Open Office, and Notepad++ all work. Vim and Emacs find and replace also support regular expressions, but for this exercise, we want you to try it in some more everyday uses. To do these find and replaces from the shell, you'll need to check out the pipelining / combining commands module.

Truncate all numbers with more than 3 digits to the first three digits

Truncate all numbers with more than 3 digits to the last three digits

Rewrite all passive voice sentences in the active voice. Make the following assumptions: A sentence is passive voice if it fits the form was by . To rewrite it in the active voice, you would write . Don't worry about capitalization. Assume everything to the left of "was" is the subject and everything to the right of "by" is the object. Assume that the verb will remain unchanged. Assume that the verb has to end in "ed" to be past participle. DO NOT change any sentence unless it has " was ed by " in it. Assume that all sentences start with a capital letter and end with a period.

Your find and replace should work on the following examples:

Sam was attacked by a computer. -> a computer attacked Sam.

Sam was attacking by a computer. -> Sam was attacking by a computer. (unchanged because "attacking" doesn't end in "ed")

The quick brown fox was jumped by a lazy dog. -> a lazy dog jumped The quick brown fox.

Sam was exploded. -> Sam was exploded. (unchanged because there is no "by")

Sam was exploded by. -> Sam was exploded by. (unchanged because there is no object after "by")

Sam was waiting for someone to have exploded by themselves. -> Sam was waiting for someone to have exploded by themselves. (unchanged because the word immediately after "was" isn't the verb)

Was exploded by Sam. -> Was exploded by Sam. (unchanged because there's no subject before "was")