将字符串拆分为标记并在 Perl 中存储分隔符

发布于 2024-08-15 13:37:08 字数 412 浏览 6 评论 0原文

我有一个像这样的字符串:

a  b   c       d

我像这样处理我的字符串:

   chomp $line;
    my @tokens = split /\s+/, $line;
    my @new_tokens;
    foreach my $token (@tokens) {    
        push @new_tokens, some_complex_function( $token );
    }
    my $new_str = join ' ', @tokens;

我想用原始空格重新连接该字符串。有什么方法可以存储拆分后的空白并在以后重新使用它吗?或者这将是一个巨大的痛苦?它主要是装饰性的,但我想保留输入字符串中的原始空格。

I have a string like this:

a  b   c       d

I process my string like this:

   chomp $line;
    my @tokens = split /\s+/, $line;
    my @new_tokens;
    foreach my $token (@tokens) {    
        push @new_tokens, some_complex_function( $token );
    }
    my $new_str = join ' ', @tokens;

I'd like to re-join the string with the original whitespace. Is there some way that I can store the whitespace from split and re-use it later? Or is this going to be a huge pain? It's mostly cosmetic, but I'd like to preserve the original spaces from the input string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深爱成瘾 2024-08-22 13:37:08

如果您使用带有捕获括号的正则表达式进行拆分,则拆分模式将包含在结果列表中(请参阅 perldoc -f 分割):

my @list = split /(\s+)/, 'a  b   c       d';
print Data::Dumper::Dumper(\@list);

VAR1 = [
          'a',
          '  ',
          'b',
          '   ',
          'c',
          '       ',
          'd'
        ];

If you split with a regex with capturing parentheses, the split pattern will be included in the resulting list (see perldoc -f split):

my @list = split /(\s+)/, 'a  b   c       d';
print Data::Dumper::Dumper(\@list);

VAR1 = [
          'a',
          '  ',
          'b',
          '   ',
          'c',
          '       ',
          'd'
        ];
耳根太软 2024-08-22 13:37:08

只需在单词边界上进行拆分:

split /\b/, $line;

对于您的示例,这将给出:

('a','  ','b','   ','c','       ','d')

编辑: 正如 brian d foy 指出的那样, \b 使用了错误的字符类,遵循我最初的想法,我想出了使用环视断言。不过,这看起来比 Ether 的答案要复杂得多:

split /(?:(?<=\S)(?=\s)|(?<=\s)(?=\S))/, $line;

Just split on word boundaries:

split /\b/, $line;

For your example, this will give:

('a','  ','b','   ','c','       ','d')

EDIT: As brian d foy pointed out, \b uses the wrong character classes, Following my original idea, I came up with using look-around assertions. This looks way more complicated than Ether's answer, though:

split /(?:(?<=\S)(?=\s)|(?<=\s)(?=\S))/, $line;
撕心裂肺的伤痛 2024-08-22 13:37:08

你为什么不简单地这样做: my $new_str = uc( $line );

更新 - 原来的 uc() 只是“更复杂的函数”的简写。

嗯,一般来说你还可以:

$line =~ s/(\S+)/more_complex_function($1)/ge;

Why don't you simply do: my $new_str = uc( $line ); ?

UPDATE - original uc() is just a shorthand for "more complex function".

Well, generally you can also:

$line =~ s/(\S+)/more_complex_function($1)/ge;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文