How do I access captured substrings after a successful regex match in Perl?

前端 未结 4 1711
余生分开走
余生分开走 2021-01-24 11:09

I am searching for a string in Perl and storing it in another scalar variable. I want to print this scalar variable. The code below doesn\'t seem to work. I am not sure what is

4条回答
  •  温柔的废话
    2021-01-24 11:38

    m// returns the captured matches in list context:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my $pattern = qr/^\s{10}([0-9]{2})\s[A-Z]/;
    
    while ( my $line =  ) {
        if ( my ($n) = $line =~ $pattern ) {
            print "$n\n";
        }
    }
    
    __DATA__
              13 E 0.496 -> Q 0.724
              18 S 0.507 -> R 0.513
              19 N 0.485 -> S 0.681
              21 N 0.557 -> K 0.482
    

提交回复
热议问题