How can I extract the nth occurrence of a match in a Perl regex?

后端 未结 7 866
执笔经年
执笔经年 2020-12-22 01:32

Is it possible to extract the nth match in a string of single-quoted words?

use strict;
use warnings;

my $string1 = \"\'I want to\' \'ex         


        
7条回答
  •  清酒与你
    2020-12-22 02:17

    The match-g operator evaluated in an array context yields an array of matches. Therefore:

    @matches = $string =~ /'(.*?)'/g;
    $matches[$index-1];
    

    is one way to get what you want.

提交回复
热议问题