How can I extract the matches from the Perl match operator into variables?

前端 未结 6 1006
无人及你
无人及你 2020-12-29 08:09

If I have a match operator, how do I save the parts of the strings captured in the parentheses in variables instead of using $1, $2, and so on?

相关标签:
6条回答
  • 2020-12-29 08:10

    The trick is to make m// work in list context by using a list assignment:

     ($interesting) = $string =~ m/(interesting)/g;
    

    This can be neatly extended to grab more things, eg:

     ($interesting, $alsogood) = $string =~ m/(interesting) boring (alsogood)/g;
    
    0 讨论(0)
  • 2020-12-29 08:12

    $& - The string matched by the last successful pattern match (not counting any matches hidden within a BLOCK or eval() enclosed by the current BLOCK).

    #! /usr/bin/perl
    
    use strict;
    use warnings;
    
    my $interesting;
    my $string = "boring interesting boring";
    $interesting = $& if $string =~ /interesting/;
    
    0 讨论(0)
  • 2020-12-29 08:25

    Use the bracketing construct (...) to create a capture buffer. Then use the special variables $1, $2, etc to access the captured string.

    if ( m/(interesting)/ ) {
        my $captured = $1;
    }
    
    0 讨论(0)
  • 2020-12-29 08:27

    You can use named capture buffers:

    if (/ (?<key> .+? ) \s* : \s* (?<value> .+ ) /x) { 
        $hash{$+{key}} = $+{value};
    }
    
    0 讨论(0)
  • 2020-12-29 08:29

    @strings goes on the left and will contain result, then goes your input string $input_string. Don't forget flag g for matching all substrings.

    my @strings=$input_string=~m/stuff (.*) stuff/g;
    
    0 讨论(0)
  • 2020-12-29 08:30

    Usually you also want to do a test to make sure the input string matches your regular expression. That way you can also handle error cases.

    To extract something interesting you also need to have some way to anchor the bit you're interested in extracting.

    So, with your example, this will first make sure the input string matches our expression, and then extract the bit between the two 'boring' bits:

    $input = "boring interesting boring";
    if($input =~ m/boring (.*) boring/) {
        print "The interesting bit is $1\n";
    }
    else {
        print "Input not correctly formatted\n";
    }
    
    0 讨论(0)
提交回复
热议问题