tom arnall wrote:
> the following script:
>
> my ($f,$g,$h);
> $_= "abcde";
> ($f,$g,$h)=/(a{0,1})(m{0,1})(b{0,1})/;
> print "scalar g defined after first regex\n" if defined $g;
> ($f,$g,$h)=/(a{0,1})(m){0,1}(b{0,1})/;
> print "scalar g defined after second regex\n" if defined $g;
>
> produces:
>
> scalar g defined after first regex
>
> why does '(m{0,1})' produce an empty scalar, while '(m){0,1}' an undefined
> scalar?
>
> and what i'm really after is to say:
>
> ($f,$g,$h)=/(a{0,1})(m|mn){0,1}(b{1,1})/;
>
> and have $g come out a scalar which is defined.
First, '{0,1}' is a verbose way of saying '?' and {1,1} is superfluous so your
expression can be written as:
( $f, $g, $h ) = /(a?)(m|mn)?(b)/;
As to your problem of capturing and grouping, use non-capturing parentheses
for the grouping part:
( $f, $g, $h ) = /(a?)((?:m|mn)?)(b)/;
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>