I'm not sure if I completely understand your question, partially because the regexps are obscured since code tags weren't used to preserve things like []. And partially because the text that came between "Now the question is this:" and the question mark, didn't really make sense to me.

From what I can tell, it looks to me like in the first regexp, you're only capturing with one set of parens. That means that $1 is the only variable that will have anything captured into it, in your first regexp. Nothing is populating $2 and $3. Also, $1 will only have something captured into it if the overall match was a success. Also, there is no need to use square brackets around \W if you're only matching one character, with a built-in character class. The square brackets are generally used to create custom character classes.

I hope this is of some help.

If your book gave you the regexp you have shown us, it would be a good candidate for the garbage heap. You might really get a lot out of perlretut, followed by perlfaq6, followed by perlre. Those three online POD's give a very thorough explanation of regular expressions, and start out (in perlretut) at a pace that isn't too overwhelming.

As far as what $1 $2 and $3 would "obviously" contain, I have no idea because you didn't provide the actual input string for us to see.

Your second regexp (the one that follows "THIS should work:" will substitute the first non-word character in the string with a hyphen. Is that what you meant by "work"? I might be missing something too. ;)


Dave


"If I had my life to do over again, I'd be a plumber." -- Albert Einstein

In reply to Re: String Substitution Operator by davido
in thread String Substitution Operator by hehenoobhehe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.