如何用perl“应用”退格字符

时间:2015-08-03 09:15:08

标签: regex perl non-printing-characters

我有一个包含多个退格字符(^H)的文件。我希望能够"申请" perl中的那些退格。我找到了一些解决方案,但在我的案例中没有一个解决方案。 关键线是这一个:

test>>M^H ^HManagement.^H^H^H^H^H^H^H^H^H^Hanagement.F^H ^HFiles.^H^H^H^H^Hiles.s^H ^Hs.^H ^Hc^H ^H^H ^Hscript.^H ^H^H^H^H^Hripts^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^Hscripts.^H.s^H ^Hshow_file ^H^H^H^H^H^H^H^H^Hhow_file = transform_factory_to_running^M

结果应如下所示:

test>>Management.Files.scripts.show_file = transform_factory_to_running^M

在vi中,我能够按照https://stackoverflow.com/a/1298728/2837411中的建议正确转换文本。 但是这个问题中也提出了perl解决方案:https://stackoverflow.com/a/1298970/2837411对我没有用(使用$_):

s{([^\x08]+)(\x08+)}{substr$1,0,-length$2}eg;

这个输出是:

test>>Management.Files.sscriptriptscripts.show_file = transform_factory_to_running^M

所有退格都消失了,但看起来好像其中一些退格应用于另一个退格?!

2 个答案:

答案 0 :(得分:2)

这只是在一个替换循环中完成

它重复删除行开头的所有空格实例(它没有效果)或非退格字符后跟退格(模拟删除前面的字符)

请注意,我必须在正则表达式模式中使用\cH而不是\b,因为后者在此上下文中是单词边界锚

use strict;
use warnings;
use v5.10;

my $s = 'M^H ^HManagement.^H^H^H^H^H^H^H^H^H^Hanagement.F^H ^HFiles.^H^H^H^H^Hiles.s^H ^Hs.^H ^Hc^H ^H^H ^Hscript.^H ^H^H^H^H^Hripts^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^Hscripts.^H.s^H ^Hshow_file ^H^H^H^H^H^H^H^H^Hhow_file = transform_factory_to_running^M';
$s =~ s/\^H/\b/g; # convert `^H` to backspace

1 while $s =~ s/(?:^|[^\cH])\cH//g;

say $s;

输出

Management.Files.scripts.show_file = transform_factory_to_running^M

更新

这是一个将字符串处理为字符流的版本,类似于simbabque's解决方案,但是从左到右代替

基本上任何退格都会从$result缓冲区的末尾删除一个字符,如果要删除一个字符,则只需追加任何其他字符

输出与上面的代码相同

use strict;
use warnings;
use v5.10;

my $s = 'M^H ^HManagement.^H^H^H^H^H^H^H^H^H^Hanagement.F^H ^HFiles.^H^H^H^H^Hiles.s^H ^Hs.^H ^Hc^H ^H^H ^Hscript.^H ^H^H^H^H^Hripts^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^Hscripts.^H.s^H ^Hshow_file ^H^H^H^H^H^H^H^H^Hhow_file = transform_factory_to_running^M';
$s =~ s/\^H/\b/g;

say apply_backspace_characters($s);

sub apply_backspace_characters {

  my $result;

  for my $c ( split //, shift ) {
    if ( $c eq "\b" ) {
      substr($result, -1) = '';
    }
    else {
      $result .= $c;
    }
  }

  $result;
}

答案 1 :(得分:0)

这是一个非常明确的解决方案,可能不是最快的。但是,它完成了工作。

sub apply_backspace_characters {
    my $string = shift;

    # replace the ^H characters with one BS char
    $string =~ s/\^H/chr(8)/ge;

    my @output;
    my $backspace_count = 0; # keep track of how many BS we have seen in a row

    # iterate over string by char from the right
    foreach my $char ( reverse split //, $string ) {
        if ( $char eq chr(8) ) {
            # it's a backspace, increase counter and skip
            $backspace_count++;
            next;
        }
        if ($backspace_count) {
            # there are still backspaces on the 'stack', decrease counter and skip
            $backspace_count--;
            next;
        }
        # no backspaces left, keep this character and put at front
        # (because we are going backwards)
        unshift @output, $char;
    }

    return join '', @output;
}

say apply_backspace_characters(
    "test>>M^H ^HManagement.^H^H^H^H^H^H^H^H^H^Hanagement.F^H ^HFiles.^H^H^H^H^Hiles.s^H ^Hs.^H ^Hc^H ^H^H ^Hscript.^H ^H^H^H^H^Hripts^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^Hscripts.^H.s^H ^Hshow_file ^H^H^H^H^H^H^H^H^Hhow_file = transform_factory_to_running^M"
);

这将输出以下内容。

test>>Management.Files.scripts.show_file = transform_factory_to_running^M