Splitting a string into tokens and storing delimiters in Perl

I have a line like this:

a  b   c       d

I process my string as follows:

   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 would like to rejoin the line with the original space. Is there a way that I can save a space from split and reuse it later? Or will it be a huge pain? It is mostly cosmetic, but I would like to keep the original spaces from the input string.

+3
source share
3 answers

If you separate using a regular expression with brackets in parentheses, the separation 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'
        ];
+15
source

:

split /\b/, $line;

:

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

: , \b , , . , Ether:

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

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

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

Well, as a rule, you can also:

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

Source: https://habr.com/ru/post/1725587/


All Articles