2012-06-25 12 views
10

मैं पर्ल विशेष चर का अर्थ जानना चाहते हैं इसका मतलब है $-[0] और $+[0]

मैं googled और पाया $- प्रतिनिधित्व करते हैं कि लाइनों की संख्या पर छोड़ दिया है पृष्ठ और $+ अंतिम खोज पैटर्न से मेल खाने वाले अंतिम ब्रैकेट का प्रतिनिधित्व करते हैं।

लेकिन मेरा प्रश्न $-[0] और $+[0] नियमित अभिव्यक्तियों के संदर्भ में है।

कोड नमूना आवश्यक होने पर मुझे बताएं।

+1

आप perlvar perldoc पढ़ा था? http://perldoc.perl.org/perlvar.html '$ + [0] 'पर उदाहरण काफी स्पष्ट है। –

+0

आपको [मैनुअल] [1] से अधिक परिचित होना चाहिए। –

+5

वे चर जो आप खोज रहे हैं वे '@ -' और' @ + 'हैं। – flesk

उत्तर

13

perldoc perlvar@+ और @- देखें।

$+[0] पूरे मैच के अंत की स्ट्रिंग में ऑफ़सेट है।

$-[0] अंतिम सफल मैच की शुरुआत का ऑफसेट है।

8

ये एक सरणी (वर्ग वर्ग और संख्या द्वारा इंगित) से दोनों तत्व हैं, इसलिए आप @ - (सरणी) के लिए खोज करना चाहते हैं, न कि $ - (एक असंबंधित स्केलर चर)।

सराहना

perldoc perlvar 

पर्ल के विशेष चरों बताते हैं। यदि आप वहां के लिए खोज करते हैं @ - आपको मिलेगा।

$-[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

4

$-[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 
+1

अच्छा उदाहरण के लिए धन्यवाद ..the @ + @ - $ - $ + की अवधारणा अब मुझे पूरी तरह स्पष्ट है ... लेकिन यह निश्चित रूप से भविष्य के आगंतुकों +1 की सहायता करेगा – AnonGeek

संबंधित मुद्दे