Skrevet av Emne: Perl: Remove ANSI sequences from a string  (Lest 4376 ganger)

ATC

  • Gjest
Perl: Remove ANSI sequences from a string
« på: 20. Oktober 2009, 13:22 pm »
  • [applaud]0
  • [smite]0
  • When processing terminal data or text files containing ANSI escape sequences, it can sometimes be useful to filter these out



    ATC

    • Gjest
    [Solved] Perl: Remove ANSI sequences from a string
    « Svar #1 på: 20. Oktober 2009, 13:22 pm »
  • [applaud]0
  • [smite]0
  • sub remove_ansi  {
      for (@_) {
        $_ =~ s/\033\[[\?|0-9|\;]+?[a-z]//gi;
      }
    }

    Use like 'chop' and 'chomp', for example:

    remove_ansi $string; # Remove ANSI from $string
    remove_ansi $a, $b; # Remove ANSI from $a and $b

    Note:
    $string = remove_ansi $string; # Does NOT do what you expect!