in reply to followup on regex

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

Replies are listed 'Best First'.
Re: Re: followup on regex
by graff (Chancellor) on Jul 22, 2003 at 03:06 UTC
    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.