我是perl的新手。我知道我可以通过unpack
或使用正则表达式来分割一些恒定数量的字符。
但有没有一些标准的方法来分割每n个字符和新行?
这是我要分割的字符串:
my $str="hello\nworld";
my $num_split_chars=2;
答案 0 :(得分:3)
也许以下内容会有所帮助:
use strict;
use warnings;
use Data::Dumper;
my $str = "hello\nworld";
my $num_split_chars = 2;
$num_split_chars--;
my @arr = $str =~ /.{$num_split_chars}.?/g;
print Dumper \@arr;
输出:
$VAR1 = [
'he',
'll',
'o',
'wo',
'rl',
'd'
];