Also, the above (like your original) won't work if anything appears after ">>" on the previous line (including spaces). For example,

>> text >>> text

gets changed to

>> text PYTHON_PROMPT text

Is that how it should be? Fixing this isn't easy, and it might be impossible without $1

Update: Well, the following doesn't use $1. However, I don't know about effeciency (since I don't have another solution with which to compare). It works, though, which is more than I can say for my attempts that did use $1.

foreach (split("==========\n", <<'__EOI__')) >>> i = q ========== YES >>> i = q ========== >>> i = q YES ========== >>> i = q >> ========== >> >>> i = q ========== >>> i = q >> No! ========== >> No! >>> i = q ========== YES >>> i = q YES >>> i = q ========== >>> i = q >> No! YES >>> i = q __EOI__
{ foreach ($_) { if (/\G (?= >>> )/xgc) { # Substitution supressed by following ">>". redo if /\G >>> [^\n]* \n (?= >> (?!>) ) /xcg; # Do substitution. s/\G >>>/PYTHON_PROMPT/xcg; # Go process next line. /\G [^\n]* \n /xcg; redo; } # Substitution prevented by preceeding ">>" /\G >> (?!>) [^\n]* \n >>> [^\n]* \n /xcg && redo; # ^^^^^ optional due to statement order. # Go process next line. m/\G [^\n]* \n /xcg && redo; } print; print("==========\n"); }

In reply to Re^2: Using a regex to catch interpreters by ikegami
in thread Using a regex to catch interpreters by eastcoastcoder

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.