vitoco has asked for the wisdom of the Perl Monks concerning the following question:
Hi!
I usually capture and assign a variable this way:
my ($id) = (m!/key/(\d+)!);
I need to add another pattern for the same capture because sometimes the string changes:
my ($id) = (m!/(\d+)-\d+!);
How could I combine both patterns to get only one capture for both possible sources? Obvously this does not work as it gets 2 captures:
my ($id) = (m!/key/(\d+)|/(\d+)-\d+!);
A workaround could be the following:
my $id = $1 // $2 if m!/key/(\d+)|/(\d+)-\d+!;
I want to know I there is a way to get only one capture from two different surrounding strings in a single pattern.
Thanks!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: capture value from two patterns
by jo37 (Curate) on Aug 20, 2020 at 20:20 UTC | |
by AnomalousMonk (Archbishop) on Aug 20, 2020 at 23:08 UTC | |
by vitoco (Hermit) on Aug 20, 2020 at 20:36 UTC | |
Re: capture value from two patterns
by Haarg (Priest) on Aug 21, 2020 at 07:42 UTC | |
Re: capture value from two patterns
by LanX (Saint) on Aug 20, 2020 at 20:42 UTC | |
by AnomalousMonk (Archbishop) on Aug 20, 2020 at 22:31 UTC | |
by LanX (Saint) on Aug 20, 2020 at 23:37 UTC |