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

前端 未结 4 1787
余生分开走
余生分开走 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:54

    JB is correct. Your regular expression would need to use captures (which are defined by parentheses) for the individual parts to be collected. If you want to capture all of the elements in your line, you would want this:

    my $find = '\s{10}([0-9]{2})\s([A-Z])';
    my $field1;
    my $field2;
    while (my $line = ) {
       chomp ($line);
       if ($line=~ /$find/) {
          $field1 = $1;
          $field2 = $2;
          # Do something with current line's field 1 and field 2
       }
    }
    

提交回复
热议问题