我只是试图在模式的基础上分割一个字符串。我尝试了以下代码,但它产生了一些未初始化的元素。谁能解释一下这里发生了什么?
my $string = 'this is the string which is going to be splitted';
my $pattern = qr/(\bis\b)|(\bthe\b)/;
my @splitted = split(/$pattern/,$string);
print join "\n#", @splitted;
输出
Use of uninitialized value $splitted[2] in join or string at D:/Testing/Context/split_un.pl line 9.
Use of uninitialized value $splitted[4] in join or string at D:/Testing/Context/split_un.pl line 9.
Use of uninitialized value $splitted[8] in join or string at D:/Testing/Context/split_un.pl line 9.
this
#is
#
#
#
#the
# string which
#is
#
# going to be splitted
答案 0 :(得分:2)
当拆分正则表达式使用捕获组时,它会将捕获的分隔符与拆分元素一起传递,其中一些未定义。
根据你的需要,这个正则表达式不会产生未定义的元素,
my $pattern = qr/(\bis\b|\bthe\b)/;