我想知道Perl特殊变量$-[0]和$+[0]的含义。
我已经搜索了一下,发现$-代表页面上剩余的行数,$+代表最后一个与最后一个搜索模式匹配的括号。
但我的问题是,在正则表达式的上下文中,$-[0]和$+[0]是什么意思。
如果需要代码示例,请告诉我。
我已经搜索了一下,发现$-代表页面上剩余的行数,$+代表最后一个与最后一个搜索模式匹配的括号。
但我的问题是,在正则表达式的上下文中,$-[0]和$+[0]是什么意思。
如果需要代码示例,请告诉我。
perldoc perlvar
本文介绍了Perl语言的特殊变量。如果您在其中搜索@-,您将找到:
$-[0]表示上一次成功匹配的起始偏移量。$-[n]表示第n个子模式匹配的子字符串的起始偏移量,如果该子模式没有匹配,则为undef。
为了更好地理解$-[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
$+[0]
的例子非常清晰。 - J-16 SDiZ@-
和@+
。 - flesk