9

perlの特殊変数の意味を知りたいのですが$-[0]$+[0]

私はグーグルで調べ$-て、ページに残っている行数を$+表し、最後の検索パターンに一致した最後のブラケットを表すことを発見しました。

しかし、私の質問は、正規表現のコンテキストで何$-[0]を意味するかです。$+[0]

コードサンプルが必要かどうか教えてください。

4

3 に答える 3

14

およびperldoc perlvarについて参照してください。@+@-

$+[0]一致全体の末尾の文字列へのオフセットです。

$-[0]最後に成功した一致の開始位置のオフセットです。

于 2012-06-25T09:55:49.923 に答える
8

これらはどちらも配列の要素 (角かっこと数字で示されます) であるため、$- (関連のないスカラー変数) ではなく @- (配列) を検索する必要があります。

お褒めの言葉

perldoc perlvar 

Perl の特殊変数について説明します。@- で検索すると出てきます。

$-[0] is the offset of the start of the last successful match. $-[n] is the offset of the start of the substring matched by n-th subpattern, or undef if the subpattern did not match.

于 2012-06-25T09:55:30.930 に答える
5

の理解を深めるために例を追加すると$-[0]$+[0]

変数に関する情報も追加$+

use strict;
use warnings;

my $str="This is a Hello World program";
$str=~/Hello/;

local $\="\n"; # Used to separate output 

print $-[0]; # $-[0] is the offset of the start of the last successful match. 

print $+[0]; # $+[0] is the offset into the string of the end of the entire match. 

$str=~/(This)(.*?)Hello(.*?)program/;

print $str;

print $+;                    # This returns the last bracket result match 

出力:

D:\perlex>perl perlvar.pl
10                           # position of 'H' in str
15                           # position where match ends in str
This is a Hello World program
 World
于 2012-06-25T13:37:22.347 に答える