Perl short form of regex capture -
i first capture group same var. in fact, looking short form of:
$_ = $1 if m/$prefix($pattern)$suffix/;
something like:
s/$prefix($pattern)$suffix/$1/a; ## option looking
or better:
k/$prefix($pattern)$suffix/; ## k option wish can use...
this avoid need of matching text leads more complicated line:
s/^.*$prefix($pattern)$suffix.*$/defined $1 ? $1 : ""/e;
any clues?
this useful example:
push @array, {id => k/.*\s* = \s* '([^']+)'.*/};
instead of
/.*\s* = \s* '([^']+)'.*/; $id = ''; $id = $1 if $1; push @array, {id => $id};
edit:
i found interesting way, if $1
not defined error :(
$_ = (/$prefix($pattern)$suffix/)[0];
my $var = /$prefix($pattern)$suffix/ ? $1 : '';
you want make sure regex matches before using capture group. using ternary can either specify default value or can warn
match wasn't found.
alternatively, can use list form of capture groups inside if statement, , let else output warning:
if (my ($var) = /$prefix($pattern)$suffix/) { ...; } else { warn "unable find match"; }
Comments
Post a Comment