I am sitting here reading through Chapter 5 of Programming Perl, and a bit on p. 203 is confusing me. Maybe.

$_ = "Paris in THE THE THE THE spring"; 1 while s/\b(\w+) \1\b/$1/gi;
The regex is a reference to earlier in the chapter, a duplicate word killer. After playing around with test patterns and test regex searches, I am not sure I really get what's going on above (perhaps it is just a lot to hit a weekend programmer like myself with at once ^_^). Here is what I think is going on: 1st regex) The while loop has a one because all the interesting stuff happens in the logical test. (\b(\w+) \1\b) matches a word boundary followed by one or more word characters followed by a space and whatever matched the (\w+). The match of (\w+) replaces the whole match. I think I kinda get it.

But were you to tell me to make a regex for that match, I'd have done this:

/(\b\w+)\1\b
Unfortunately I'm not really good enough to divine the difference between the two. Also, I don't see why the substitution that the book does doesn't eat up the preceding space: "Paris THE THE" turns into "ParisTHE". I hope I've a real conceptual problem here, and not just a brain fart, because I'll be feeling really sheepish asking if it is indeed a brain fart. Thanks in advance!

In reply to Prog. Perl 3rd Ed. Regex Question by makar

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.