I
Irving Kimura
The documentation for @- in perlvar says
One can use "$#-" to find the last matched subgroup in the last
successful match. Contrast with $#+, the number of subgroups
in the regular expression.
....which suggests that it is possible for $#- and $#+ to be different,
but I have not been able to produce an example in which they are
actually different. In fact, later in the documentation for @-,
perlvar says
After a match against some variable $var:
$` is the same as "substr($var, 0, $-[0])"
$& is the same as "substr($var, $-[0], $+[0] - $-[0])"
$' is the same as "substr($var, $+[0])"
$1 is the same as "substr($var, $-[1], $+[1] - $-[1])"
$2 is the same as "substr($var, $-[2], $+[2] - $-[2])"
$3 is the same as "substr $var, $-[3], $+[3] - $-[3])"
....which implies that $-[n] is defined if and only if $+[n] is
defined, meaning that $#- and $#+ should always be equal. What
gives?
In addition, the above quote suggests that all elements of @- are
defined (if some element of @- were undefined, the corresponding
substr expression would be nonsensical), but the docs say that
there are cases in which some elements of @- are undefined. Although
I've managed to come up with examples in which, for some n, $-[n]
and $+[n] are equal (implying an "empty submatch"), I have not been
able to produce an example in which @- has undefined elements.
But more important than an example is the answer to this question:
which regexps can produce empty submatches (both $-[n] and $+[n]
are defined and equal), and which can produce "undefined" ones
(both $-[n] and $+[n] are undefined)? Can the same parenthesized
group in a regexp produce either an empty submatch or an undefined
one depending on the input string?
Thanks!
Irv
One can use "$#-" to find the last matched subgroup in the last
successful match. Contrast with $#+, the number of subgroups
in the regular expression.
....which suggests that it is possible for $#- and $#+ to be different,
but I have not been able to produce an example in which they are
actually different. In fact, later in the documentation for @-,
perlvar says
After a match against some variable $var:
$` is the same as "substr($var, 0, $-[0])"
$& is the same as "substr($var, $-[0], $+[0] - $-[0])"
$' is the same as "substr($var, $+[0])"
$1 is the same as "substr($var, $-[1], $+[1] - $-[1])"
$2 is the same as "substr($var, $-[2], $+[2] - $-[2])"
$3 is the same as "substr $var, $-[3], $+[3] - $-[3])"
....which implies that $-[n] is defined if and only if $+[n] is
defined, meaning that $#- and $#+ should always be equal. What
gives?
In addition, the above quote suggests that all elements of @- are
defined (if some element of @- were undefined, the corresponding
substr expression would be nonsensical), but the docs say that
there are cases in which some elements of @- are undefined. Although
I've managed to come up with examples in which, for some n, $-[n]
and $+[n] are equal (implying an "empty submatch"), I have not been
able to produce an example in which @- has undefined elements.
But more important than an example is the answer to this question:
which regexps can produce empty submatches (both $-[n] and $+[n]
are defined and equal), and which can produce "undefined" ones
(both $-[n] and $+[n] are undefined)? Can the same parenthesized
group in a regexp produce either an empty submatch or an undefined
one depending on the input string?
Thanks!
Irv