您編寫正則表達式的方式與.*
是貪婪還是非貪婪無關。 它仍然會匹配。
原因是您在.*
和\w+
之間使用了\b
。
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']
'$'是一個零寬度斷言;我認爲沒有任何消費(如果可能的話)字符串的結尾將是一個問題。參見:http://codepad.org/CwTlhn3z – NullUserException
@NullUserException我也這麼認爲,但[^(\ w +)\ b(。*)\ b(\ w *)$](http://rubular.com/r/XczcfFd2Va)仍將'expression'與最後一組匹配。如我所料,使字邊界可選將導致空匹配。我不太確定實際上發生了什麼...... – verdesmarald
看起來,Ruby的正則表達式引擎與Perl的不同。 – NullUserException