Update Copyright Years

At the start of every year, you have to update the year in the copyright statements on your web site and other published materials. If you forget this, your web site will look outdated.

There are several reasons why this seemingly trivial task can be quite tedious:

  1. You probably have a lot of files to update, with copyright statements in different places. So you want to automate it.
  2. You cannot just search and replace the year number, because historic dates should not be updated.
  3. Different files may have a different style of copyright statement, such as a © HTML element rather than the © character.
  4. You may have forgotten to update some statements in the past. You want to make sure to update those now.
  5. The first year in the copyright statement will be different for different projects. This year should not be changed.

In PowerGREP, you can solve this problem easily:

  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. Open the PowerGREP5.pgl library file included with PowerGREP. You can find it in the folder where PowerGREP is installed, c:\Program Files\Just Great Software\PowerGREP 5 by default.
  4. Select “Update copyright statements” in the library, and click the Use Action button.
  5. Set the replacement to \1- (backslash, one, dash) followed by the current year.
  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 update the copyright statements.

This was all so easy, because the regular expression we used had already been created. Writing the regular expression to take into account the problems mentioned above is the hard part.

How The Regular Expression Works

The regular expression we used is (copyright +(©|\(c\)|©) +\d{4})( *[-,] *\d{4})*. We take care of problem 2 by not just searching for the year, but for the complete copyright statement. We solve problem 3 by having the regex search for different styles, and by putting the actual copyright statement in a backreference. Problem numbers 4 and 5 are solved by only putting the first year in the backreference that we use in the replacement.

In the replacement we used \1 which is replaced by the text matched by the part between the first set of parenthesis in the regular expression. In our case: copyright +(©|\(c\)|©) +\d{4}. This regular expression matches the literal text “copyright” followed by one or more spaces, followed by either the real copyright symbol ©, the textual representation (c), or the HTML character ©. The symbol must be followed by one or more spaces, and 4 digits.

The first part of the regular expression will successfully match a copyright statement in the form of “Copyright (c) 1999”.

However, some statements may have the form “Copyright (c) 1999, 2000, 2001” or “Copyright (c) 1999-2002”. In either case, the first part of the regular expression will match “Copyright (c) 1999”. So we need to add a second part to the regular expression to match the additional years. We will put this part outside of the first parenthesis so it will be excluded from the replacement text.

We match the additional years with: ( *[-,] *\d)*. This will match zero or more spaces, followed by a dash or a comma, followed by zero or more spaces, followed by 4 digits. All of this can appear zero or more times.

If we use \1-2016 as the replacement, we will replace the entire copyright statement with all the years by the same copyright statement with only the first year, followed by -2016. So both statements mentioned two paragraphs earlier will be replaced by “Copyright (c) 1999-2016”. We maintained the style and the first year, and updated the year even if the first copyright statement wasn’t updated the past few years.

Here is a visual representation of how the original text is matched by the regular expression and turned into the final text by the replacement text with the backreference \1.

Replacing copyright statements with a regular expression