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”.

  1. Select the files you want to search through in the File Selector.
  2. Select a file format configuration such as “none” or “writable proprietary formats” that does not enable and read-only converters.
  3. Set the action type to “search and replace”. Leave the search type as “regular expression”.
  4. Enter the regular expression \w++. This regular expression matches a single word.
  5. 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.
  6. Set the target and backup file options as you like them.
  7. Click the Preview button to run a test.
  8. 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.

  1. Select “search for sections” in the “file sectioning” drop-down list.
  2. 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.
  3. Click the Preview button to run a test.
  4. 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.