How can I extract the nth occurrence of a match in a Perl regex?
- by Zaid
Is it possible to extract the n'th match in a string of single-quoted words?
use strict;
use warnings;
my $string1 = '\'I want to\' \'extract the word\' \'Perl\',\'from this string\'';
my $string2 = '\'What about\',\'getting\',\'Perl\',\'from\',\'here\',\'?\'';
sub extract_quoted {
my ($string, $index) = @_;
my ($wanted) = $string =~ /some_regex_using _$index/;
return $wanted;
}
extract_wanted ($string1, 3); # Should return 'Perl', with quotes
extract_wanted ($string2, 3); # Should return 'Perl', with quotes