0

I'm working on an open source project that needs it's copyright headers brought up to date. Currently every file contains a commented header with various years. Some correctly say 2015, others say anything from 2011 to 2014 and some provide a date range (i.e. 2011-2014 - see example included).

I would like to programmatically change all out of date years to read as 2015. Is it possible to find and replace the second year only? I'm currently using this as my regex to grab each header:

\/\*(?:(?!\*\/).|[\n\r])*\*\/

And here is a sample header:

 /**
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2011-2013 Company Name AS. All rights reserved.
 *
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the License). You may not use this file except in
 * compliance with the License.
 *
 * You can obtain a copy of the License at
 * http://someurl.com
 * See the License for the specific language governing
 * permission and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * Header Notice in each file and include the License file
 * at http://someurl.com/license.html
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 */
Mr. Meeseeks
  • 1,841
  • 2
  • 21
  • 37
  • 2
    Check [this code](http://jsfiddle.net/9yewarLy/). – Wiktor Stribiżew Oct 22 '15 at 20:37
  • 1
    After you have grabbed the header, you could perform another regex on it to replace the year ... there are a lot of online regex testers that you could use for this. A regex that I came up with would be something like `/(-?20[0-9]{2} )/g` – Mr. Meeseeks Oct 22 '15 at 20:39
  • 1
    @Meeseeks: you can take further advantage of the fact that "anything from 2011 to 2014" is to be changed. – Jongware Oct 22 '15 at 20:41
  • 2
    Now, does [this code](http://jsfiddle.net/9yewarLy/3/) work for you? Just use `/(\/\*(?:(?!\*\/)[^])*)\b\d{4}(?:-\d{4})?\b/`. Perhaps, a simpler regex can also be used: `/^(\s*\*\s*Copyright\s*\(c\)\s*)\b\d{4}(?:-\d{4})?\b/m`. – Wiktor Stribiżew Oct 22 '15 at 20:42
  • 1
    @Kenney That warning applies to people **receiving** the files, not the authors themselves. – Barmar Oct 22 '15 at 20:45

1 Answers1

1

As per @stribizhev's comment(s)

If you want to replace:

  • 2011-2014 turns into 2011-2015
  • 2014 turns into 2015
  • 2011 to 2014 turns into 2011 to 2015
  • And possibly more (if you're not careful)

then you could use:

.replace(/(\/\*(?:(?!\*\/)[^])*)\b(-?\d{4} )\b/, "$12015 ")

This is not a very narrow regex as it could replace more than what is expected.

I recommend testing this out in an online regex tester first.

Mr. Meeseeks
  • 1,841
  • 2
  • 21
  • 37
  • 1
    @Jongware I believe that is what OP was asking: "Is it possible to find and replace the second year only?" – Mr. Meeseeks Oct 22 '15 at 20:52
  • This is very close but it doesn't seem to work on a single year. I'm going to try using this in an online regex tester. –  Oct 22 '15 at 21:46