in reply to Re: Removing ANSI Color Codes
in thread Removing ANSI Color Codes

I'm confused by your use of (?> ... ) here, aristotle... any chance you could clear it up? (I did read the description in perlre; it didn't help clear things up). Specificly, what does this pattern accomplish that s/\e\[\d+(;\d+)*m//g; doesn't?


Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Replies are listed 'Best First'.
Re^3: Removing ANSI Color Codes (atomic grouping)
by Aristotle (Chancellor) on May 31, 2003 at 18:36 UTC

    Nothing, in this case. *g* It will just fail a notch faster in cases where it can't match.

    The reason is that once the (;\d+)* stops, if what follows isn't an m, the regex engine will backtrack, giving up a bit of what (;\d+)* matched, trying to find an m. Of course we know that neither the semicolon nor \d can match something that is an m, so no backtracking in the world is going to help and make it match.

    What (?>re) does is throw away all the intermediate states once re has matched, so if backtracking seems necessary, the engine will not remember how to backtrack into the middle of re. Effectively, if the engine fails to find an m after the (?>re), it will unmatch re all at once, rather than waste time doing so character by character.

    Makeshifts last the longest.