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:
In PowerGREP, you can solve this problem easily:
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.
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.