जिस तरीके से आपने अपना regexp लिखा है, इससे कोई फर्क नहीं पड़ता कि .*
लालची या गैर लालची है। यह अभी भी मेल खाता है।
कारण यह है कि आपने .*
और \w+
के बीच उपयोग किया था।
use strict;
use warnings;
my $string = 'this is a regular expression';
sub test{
my($match,$desc) = @_;
print '# ', $desc, "\n" if $desc;
print "test(qr'$match');\n";
if(my @elem = $string =~ $match){
print ' 'x4,'[\'', join("']['",@elem), "']\n\n"
}else{
print ' 'x4,"FAIL\n\n";
}
}
test(qr'^ (\w+) \b (.*) \b (\w+) $'x, 'original');
test(qr'^ (\w+) \b (.*+) \b (\w+) $'x, 'extra-greedy');
test(qr'^ (\w+) \b (.*?) \b (\w+) $'x, 'non-greedy');
test(qr'^ (\w+) \b (.*) \b (\w*) $'x, '\w* instead of \w+');
test(qr'^ (\w+) \b (.*) (\w+) $'x, 'no \b');
test(qr'^ (\w+) \b (.*?) (\w+) $'x, 'no \b, non-greedy .*?');
# original
test(qr'(?^x:^ (\w+) \b (.*) \b (\w+) $)');
['this'][' is a regular ']['expression']
# extra-greedy
test(qr'(?^x:^ (\w+) \b (.*+) \b (\w+) $)');
FAIL
# non-greedy
test(qr'(?^x:^ (\w+) \b (.*?) \b (\w+) $)');
['this'][' is a regular ']['expression']
# \w* instead of \w+
test(qr'(?^x:^ (\w+) \b (.*) \b (\w*) $)');
['this'][' is a regular expression']['']
# no \b
test(qr'(?^x:^ (\w+) \b (.*) (\w+) $)');
['this'][' is a regular expressio']['n']
# no \b, non-greedy .*?
test(qr'(?^x:^ (\w+) \b (.*?) (\w+) $)');
['this'][' is a regular ']['expression']
स्रोत
2012-10-14 04:38:08
'' $ एक शून्य चौड़ाई दावा है; मुझे कुछ भी खपत नहीं लगता है (यदि यह संभव है तो) स्ट्रिंग का अंत एक मुद्दा होगा। देखें: http://codepad.org/CwTlhn3z – NullUserException
@NullUserException मैंने इतना सोचा लेकिन [^ (\ w +) \ b (। *) \ B (\ w *) $] (http://rubular.com/r/XczcfFd2Va) अभी भी अंतिम समूह में 'अभिव्यक्ति' से मेल खाता है। जैसा कि मैंने अपेक्षा की थी, शब्द को वैकल्पिक परिणामों को एक खाली मैच में बनाना। मुझे सच में यकीन नहीं है कि वास्तव में क्या चल रहा है ... – verdesmarald
ऐसा लगता है कि रुबी का रेगेक्स इंजन पर्ल से अलग है। – NullUserException