in reply to Using a regex to catch interpreters

I see three problems:

1)The [^>] in (?=[^>]*\n) can match newlines.

2) ">" isn't allowed anywhere on the ">>>" line (after the ">>>"). Is that intentional?

3) The second negative lookahead is currently anchored to the character right after the ">>>", not to the start of the next line.

Fixes:

s/ (?<!^>>[^>]) ^>>> (?= [^\n]*\n # <-- removed < and added first \n (?!^>>[^>]) # <-- moved into (?=...) ) /PYTHON_PROMPT/xmg;

Updated

Replies are listed 'Best First'.
Re^2: Using a regex to catch interpreters
by ikegami (Patriarch) on Jul 06, 2005 at 06:52 UTC

    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 ($_) { 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"); }