While reading Learning Perl, I came across a problem about regular expressions. In Chapter 9, exercise 3 asks the student to use regex to replace a string a with another string b and replace b with a, e.g.:
if a=’hitman’ and b=’assassin’, then ‘A hitman murdered an assassin’ should be modified to ‘A assassin murdered an hitman’.
In the book the answer is to delete all the eoln and then use eoln as a place holder to deal with this situation, but this method seems to be clumsy and is not universally applicable. Maybe someone could have a better, e.g., a one-liner, to solve this one?
Or using the /e modifier:
That second example detects whether hitman was found. If so, substitute ‘assasin’. If not, substitute ‘hitman’.