in reply to Re: Using a regex to catch interpreters
in thread Using a regex to catch interpreters

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"); }