In another thread of mine someone responded with a snippet that I've been trying to wrap my head around ever since. This subject is a major tangent from the other thread, so I think it should be on its own. I hope that's OK.

Here is the original snippet. It is the contents of the s// regexp operator that has me completely stumped.

%d = ( foo => sub { "[@_]"; }, bar => sub { "(@_)"; }, ); $_ = 'foo fooz bar baz1'; s/(\w+)(?(?{ !$d{$1} })(?!))/$d{$1}->($1)/ge; print "$_\n";

Here is the output.

[foo] [foo]z (bar) baz1

I added comments to the code in my attempt to understand what was happening. Most of my explanatory text is contained therein.

s/ (\w+) # One or more word chars in capture buffer 1. Got it. ( # Embedding a regexp modification character or what? # So everything within this set of parenthesis is # supposed to evaluate to either m, s, i, x, p, g, or c? # I don't get it. # Also, why is all this on the left side of s\\? Why are we # _matching_ this? ?(?{ !$d{$1} }) # This is really just used as a boolean to # indicate that a hash key exists. I get it, # kind of. (?!) # From perldoc, "a zero-width negative look-ahead # assertion. I might understand this if only there was # a pattern after the "?!". ) /$d{$1}->($1)/gex; print "$_\n"; # Here is my walkthrough for 'foo'. # 1. (\w+) matches 'foo'. # 2. $d{$1} is _true_. !_true_ = false. In this case, false is "1". # 3. (?!) says to *not* match a "1" followed by null. Maybe? # 4. (?...) means use the 1 not followed by null as a regexp modifier # to 'foo'? What?!

A few of my big failures to comprehend I think are:

  1. My thinking that the outer set of *(?...)* is the use of a regexp mod. But if it's not that, then what is it?
  2. The *(?!)* without a pattern following it. Does he really want to match any '1' without a following null?
  3. Why are these on the left side of the s//? Why are we matching something other than the word?

Thanks.

EDIT: I updated the $_ var to include the "baz1" string.


In reply to Regular expression operators (?{code}), (?!pattern), and (?...) by jffry

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.