Replace HTML Attributes

This was one of the most complicated examples in the documentation that shipped with PowerGREP 1.0. Like most grep tools, PowerGREP 1.0 was not able to search through only certain sections of files. Now that PowerGREP has this ability, replacing HTML attributes is very straightforward. Makes you wonder why PowerGREP is the only Windows grep tool to support file sectioning.

When editing a web site, you may want to update some HTML tags to give the site a more consistent look. Suppose you have some tables on your web site with different background colors, and you want to give all of them the same color. However, you only want to update the “bgcolor” attribute of the tables. All the other attributes should remain unchanged.

  1. Select the files you want to search through in the File Selector.
  2. Make sure the file format configuration searches through the raw (unconverted) contents of HTML files. The predefined “None” configuration is one that does this.
  3. Start with a fresh action.
  4. Set the action type to “search and replace”. Leave the search type as “regular expression”.
  5. Select “search for sections” from the File Sectioning drop-down list. Leave the search type as “regular expression”.
  6. In the Section Search box, enter the regular expression <table[^>]*> and make sure to leave “case sensitive search” off.
  7. In the search box of the main part of the action, enter the regular expression bgcolor=([_a-z0-9]+|'[^\\']*'|"[^\\"]*") and make sure to leave “case sensitive search” off. This regular expression matches any bgcolor attribute with an unquoted value, or a single-quoted value, or a double-quoted value.
  8. Enter bgcolor=blue in the Replacement box. Each bgcolor attribute will be replaced with whatever you enter in the Replacement box.
  9. Set the target and backup file options as you like them.
  10. Click the Preview button to run a test.
  11. If all looks well, click the Replace button to actually replace “bgcolor” attributes in “table” tags.

You can find this action in the PowerGREP5.pgl standard library as “Replace HTML attributes”.

If you’re curious, with a basic grep tool that can only search-and-replace using one regular expression, this is the search pattern to use:

(<table([\s\r\n]+[a-z]+(=([_a-z0-9]+|'[^\']*'|"[^\"]*"))?)*)([\s\r\n]+bgcolor=([_a-z0-9]+|'[^\']*'|"[^\"]*"))?(([\s\r\n]+[a-z]+(=([_a-z0-9]+|'[^\']*'|"[^\"]*"))?)*[\s\r\n]*>)

The replacement text would be \1 bgcolor=blue \7

You can see the same regular expression we used to match the bgcolor attribute in the middle of this behemoth regex. All the other stuff is for matching the table tag around the attribute. It works, but PowerGREP’s sectioning abilities do make life a lot easier.