I might have obfuscated the real problem in my previous question, so here it is:
Let's suppose that you want to know if the characters from 7
to 15
are all 1
in a string:
my $str = "011000001111111111000011111110110111110000101010111";
my $i = 7, $j = 15; # => start and end positions of the substring
my $l = $j - $i + 1; # => length of the substrings
I have thought of a few reasonable ways for doing the check
# 1. with `substr` and a regex:
printf "#1: %d\n", substr($str,$i,$l) =~ /\A1*\Z/;
# 2. with a regex from the beginning of the string:
printf "#2: %d\n", $str =~ /\A.{$i}1{$l}/;
# 3. with a regex and `pos`:
pos($str) = $i;
printf "#3: %d\n", $str =~ /1{$l}/;
I expect all results to be 0
(false) but I get:
#1: 0
#2: 0
#3: 1
What would be the correct regex when using pos
in #3?