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

Hey Perl Monks, Here's a followup on a question i asked earlier. I wanted to appended the current year to a string such as copyright 2000-2001 blah blah to copyright 2000-2001, 2003 blah blah. With some perl monk help, they came up with this:

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

Now i'd like to generalize the search pattern to contain the word copyright and any sequence of years seperated by hyphens, commas and periods,

eg "copyright 1999, 2000-2001, 2002. blah blah blah"

and insert the current year right after the last number specified. In the above example, this would be

"copyright 1999, 2000-2001, 2002, 2003. blah blah blah "

How would i modify the above script?

Thanks so much for everyone's input.

JC

Replies are listed 'Best First'.
Re: followup on regex
by sauoq (Abbot) on Jul 21, 2003 at 19:34 UTC
    s/(copyright\s+[\d,-\s]*)/$1, 2003/i

    I gave that to you and I should apologize for doing so. That was a hastily typed line of code and I didn't test it at all. Sorry. That hyphen should have been at the end of the character class and you probably only want to match literal spaces in your list of years. Change that to

    s/(copyright\s+[\d, -]*)/$1, 2003/i
    and it will work mostly as I originally intended.

    That should do what you are asking, but be aware that it is very liberal in what it accepts. Perhaps overly so. It doesn't do any error checking at all and just makes the assumption that any string of digits, spaces, commas, and hyphens after the word "copyright" will be a list of years. In other words, it would happily accept "copyright -- 1, 2, 3".

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: followup on regex
by Wonko the sane (Curate) on Jul 21, 2003 at 19:24 UTC
    Your original regex has a flaw in the character class.

    The '-' needs to be escaped.

    This is a corrected pattern that should do what you want.

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

    Wonko

Re: followup on regex
by jc23 (Acolyte) on Jul 21, 2003 at 20:21 UTC
    Hi,

    Wonko, thanks for the help. It works perfectly! Do you mind explaining the syntax to me? I'd like to know what I'm doing in case I'll need to do something similar in the future.

    Sauoq, thanks for the corrections. Actually what your code does is add the current year right after the word "copyright". So in essence, it does the exact opposite of wonko's code. That could also come in handy. Thanks for your help!

    jc

      The output of "perldoc perlre" (or "man perlre" on unix) is lengthy (and rather dense in spots), but well worth whatever time you can devote to read and understand it. It will explain the syntax: what the square brackets mean inside a regex, why the positioning of a hyphen inside square brackets is very important, when you need to use a backslash to "escape" a character (in square brackets or elsewhere in the regex), and when you don't need to use it.

      For parts that seem hard to decipher, play with the examples that are provided; try variations on the examples to see what happens; perl one-liners on a command line are very handy for this:

      perl -e '$_ = "test-string"; print "okay\n" if (/[-st]{5}/)' # that should print "okay" perl -e '$_ = "test-string"; print "okay\n" if (/[s\-t]{5}/)' # should do the same perl -e '$_ = "test-string"; print "okay\n" if (/[s-t]{5}/)' # prints nothing: regex didn't match perl -e '$_ = "test-string"; print "okay\n" if (/[t-s]{5}/)' # prints an error message: "Invalid [] range "t-s" in regex...
      ... and so on. It all makes sense when you RTFM.