I don't understand the effort to use split and map in solutions.
As the one who first introduced them, I wasn't making any special effort to use split or map. As I (mis)understood the spec, it was the most natural approach to a solution. At a human-level description, if you have a string and want to put brackets around each character, do you search the string for characters and, each time you find one, change it to a three-character sequence of the same character preceded and followed by brackets ($word =~ s/./[$1]/g); or do you consider the text as a list of characters (split '', $word) and put brackets around each of them (map { "[$_]" }, @chars)? Personally, if I were doing it by hand, I'd take the latter approach, and I expect pretty much anyone else would, too.

But, as already pointed out, I misunderstood the spec as wanting to bracket all characters, not only letters. With that limitation in place, the "treat it as a list of characters, not searching in a string" approach still works with a minor modification, and much more simply than the earlier attempts to fix it:

$new_word = join "", map { $_ =~ /[a-zA-Z]/ ? "[$_]" : $_ } split "", +$word;
Again following the "how would I do it by hand?" approach, this changes the map from "put brackets around each character" to "put brackets around characters in a-zA-Z and leave any other characters as-is", thus transforming "layer123" to "[l][a][y][e][r]123".

In reply to Re^9: regex find and replace with a twist by dsheroh
in thread regex find and replace with a twist by Anonymous Monk

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.