Capitalize The First Letter of Each Word
PowerGREP’s match placeholders make it easy to change the case of the replacement text in a search and replace action, or the text to be collected in a “collect data” action. This example shows how you can capitalize the first character in each word. For a more generic example, see “padding replacements”.
- Select the files you want to search through in the File Selector.
- Select a file format configuration such as “none” or “writable proprietary formats” that does not enable and read-only converters.
- Set the action type to “search and replace”. Leave the search type as “regular expression”.
- Enter the regular expression \w++. This regular expression matches a single word.
- Enter %MATCH:F% as the replacement text. This match placeholder inserts the entire regular expression match, with the first character converted to upper case and the rest to lower case.
- Set the target and backup file options as you like them.
- Click the Preview button to run a test.
- If all looks well, click the Replace button to actually capitalize the words.
Capitalizing Words in Strings
In practice, you’ll often want to capitalize only certain words in the file rather than all words. The example below shows how to capitalize words inside double-quoted strings only. PowerGREP’s file sectioning feature makes this easy. The first six steps are the same as in the previous action.
- Select “search for sections” in the “file sectioning” drop-down list.
- Enter the regular expression "[^"\r\n]++" or one of the other regexes for matching strings in the “section search” box. This regular expression matches a double-quoted string that does not span across lines. Double quotes cannot appear inside the string.
- Click the Preview button to run a test.
- If all looks well, click the Replace button to actually capitalize the words.
That’s all there’s to it. The file sectioning feature simply restricts the main action to the parts of the file matched by the file sectioning regular expression. There’s no need to modify the main action.