Reputation: 1057
In the below code, I am trying to split between two numbers. The splitting is not functioning correctly. For eg: when I split between 0 and 1, the split also occurs in between digits of 10 and 11.
Can some one help me in this?
use strict;
use warnings;
my $j= "0,2,3,6,5,1,4,7,8,12,11,10,9";
my $k=1; my $k1=0,;
my @ar=split(/($k|$k1)/,$j);
print join(";",@ar);
Expected result:0;2,3,6,5;1;4,7,8,12,11,10,9
Above code result: 0;,2,3,6,5,;1;,4,7,8,;1;2,;1;1;,;1;0;,9
Upvotes: 1
Views: 112
Reputation: 385645
First, you don't want to split on 0 or 1, you want to split on a commas next to the number 0
or 1
.
split /(?<!\d)(?:0|1)\K,|,(?=(?:0|1)(?!\d))/
Upvotes: 1
Reputation: 5135
Try
split /(\b$k\b|\b$k1\b)/, $j
\b
is the zero width word boundary delimiter.
Upvotes: 0