jc23 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Im pretty new to perl, so i was wondering if anyone could help me with a regex problem:

Given a string like "copyright 1990-2000, 2001. All rights reserved ..." I want to be able to search for the string containing the word copyright and then modifying the dates following it to read 1990-2003.

I found the regex that takes a list of numbers and produces the range as a result, but how do I modify it so that the regex will only modify the string containing the word copyright in it and modify the range to include the current year?

here's the link to the regex i was referring to click!

Thanks. jc

Replies are listed 'Best First'.
Re: modifying list of numbers
by sauoq (Abbot) on Jul 17, 2003 at 23:28 UTC
    my $year = 2003 s/(copyright\s+)(\d{4})(?:-\d{4})?/$1 . ($2 eq $year ? $2:"$2-$year")/ +ie;

    That won't handle something like "copyright 1990, 1991" though. Nor will it handle "Copyright (C) 1990-2003".

    -sauoq
    "My two cents aren't worth a dime.";
    
      Hey sauoq,

      thanks for the advice. How could i modify your code to have it pick up the entire string and just append to the end of the list? I had something like :

      s/[cC]opyright*.*?$/$&,$year/g

      but that doesnt pick up in cases where there are extra words after the year such as "Copyright 1990-2002, blah blah more text here" Instead I need something that will make it "Copyright 1990-2002,2003, blah blah more text here"

      thanks much! jc

        That would be a little less complicated actually. How about something like:

        s/(copyright\s+[\d, -]*)/$1, 2003/i;
        ?

        Edit: Changed character class to use a literal space and (oops) put the hyphen in the right place: at the end.

        -sauoq
        "My two cents aren't worth a dime.";
        
•Re: modifying list of numbers
by merlyn (Sage) on Jul 18, 2003 at 00:39 UTC
    If my recollection of the copyright law is correct, you actually aren't permitted to make that sort of edit. You have to have an explicit list of all the years that any portion of the document was created for which you seek copyright protection.

    Also, for all intents and purposes, the words "All Rights Reserved" are no longer needed. That is an edit you can make. {grin}

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      This is definitely one of those "I Am Not A Lawyer" areas. "The copyright law", Randal? There's only one now, in one jurisdiction? : )

      I agree that the poster should at least check that what they're doing is correct before someone comes back from the Legal Department to say "aha, hey, you know what..?" and makes them do it again.

      More to the point, a better idea would be to replace with some kind of solution like a server-side include, if this is a website we're talking about. We long ago converted all pages to use "copyright.txt" at the bottom for just this reason.



      ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
        There's only one now, in one jurisdiction?
        For the countries that have signed into the Berne Convention, yes.

        That's why "all rights reserved" is no longer needed, for example.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

      Oh okay. I was just trying to make things simplier. In that case, if I must list all dates... how would I append the current year to the end of the list? What modifications would i need for this code:
      my $year = 2003 s/(copyright\s+)(\d{4})(?:-\d{4})?/$1 . ($2 eq $year ? $2:"$2-$year")/ +ie; <p>
      Also can someone please explain what the above does?

      thanks! jc

Re: modifying list of numbers
by JamesNC (Chaplain) on Jul 18, 2003 at 04:14 UTC
    Another example:
    Just add another substitution foreach case...
    use strict; while(<DATA>){ ~s/copyright\s+(\d+)-(\d+),\s+(\d+)/copyright $1-$3/i; print; } __DATA__ Given a string like "copyright 1990-2000, 2001. All rights reserved .. +. OUTPUT: Given a string like "copyright 1990-2001. All rights reserved ...

    JamesNC