Answers to Part 4 Exercises | |
Solution to k37 Problem | |
% substitute /\(.*\)k37\(.*k37\)/\1q53\2 | |
will do it. When there are two or more wild cards in a search | |
pattern, the editor finds the longest match for the first one that | |
allows any match for the subsequent ones, and so on. | |
Solution to Capitalization Problem | |
237 , 289 substitute :[^ ]*:\L\u&:g | |
Since the "\u" and "\l" metacharacters have limited reach, they | |
don't end the effect of a "\L" or "\U" metacharacter--they only | |
make a one-character exception. | |
Author's Note: Since I wrote this solution, I have learned that | |
it will not work under many implementations of the editor. So, | |
I've added below an alternate solution, more complex but more | |
powerful, that should work everywhere. | |
237 , 289 substitute :\([A-Za-z]\)\([A-Za-z-]*\):\u\1\L\2:g | |
Even in editing environments where my first solution would work, | |
the second one has certain advantages: | |
The second solution is not fooled by situations where | |
a punctuation mark leads off a word. | |
It's up to you whether the second solution does or does not | |
capitalize any but the first word in hyphenated compounds such | |
as Frankfurt-am-Main. If you want to capitalize those subsequent | |
words, just remove the last of the three hyphens within the | |
second character class in my alternate solution. | |
Answer to No-Op Problem | |
The editor thinks a substitution has been successfully made | |
whenever the outgoing search pattern is matched and no error | |
condition is encountered, even if it is only replacing nothing | |
with nothing. So when the editor finishes up by reporting the | |
number of substitutions it has made, it is giving me a count of | |
the empty lines in my file, which tells me how many paragraphs | |
plus headlines plus list items I have. Then I divide this number | |
into the count of lines in the file, to see whether my | |
paragraphs have gotten too long. | |
Solution to HTML tags problem | |
Where the substitution command to add a tag at the start of | |
a paragraph presently removes and then replaces any first | |
character on the line, now it should remove and replace anything | |
except a "<" character in that position. If that first | |
character is a "<" it is necessary that the substitution command | |
fail so it will change nothing. To arrange this, replace that | |
period in the search pattern with a negative character class -- | |
"[^<]" -- which matches any character except the one that begins | |
every HTML tag. | |
Similarly, replace the period in the other substitution pattern | |
with the negative character class that matches any character | |
except the one that always ends an HTML tag. Now the two | |
commands will look like this: | |
global /^$/ + substitute /^[^<]/<P>&/ | |
global /^$/ - substitute :[^>]$:&</P>: | |
Back to Part 4 | |
Back to the index |