Note while using regexes for substitutions/modifications it is important to focus around the portion of the string you want to modify. Here is a short regex to do what you want (in perl), that illustrates this idea with your data.

Assuming $line contains the line you want to modify

my $two=2;
$line =~ s/(,\s+)\d+/\1$two/;

The regex looks for the first comma, matches that and a arbitary number of spaces following the comma. This is remembered in the first back reference. After that it matches a arbitary number of digits. Finally it replaces what was matched by the string in the first backreference followed by 2. Applying this on your sample data gives
[0, 1, 0, 1] becomes [0, 2, 0, 1]
[1, 1, 1, 1] becomes [1, 2, 1, 1]