为什么 .* 不使用这个 Perl 正则表达式中的整个字符串? [英] Why doesn't the .* consume the entire string in this Perl regex?

查看:51
本文介绍了为什么 .* 不使用这个 Perl 正则表达式中的整个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么第一个打印语句没有输出我期望的内容:

Why doesn't the first print statement output what I expect:

first = This is a test string, sec = This is a test string 

既然 * 和 + 都是贪婪的,为什么第一个匹配中的内部 * 即((")不消耗整个字符串?

Since both * and + are greedy, why does the the inner * i.e. inside the "((" in the first match not consuming the entire string?

use strict;
use warnings;

my $string = "This is a test string";
$string =~ /((.*)*)/; 
print "first = $1, sec = $2\n";  #prints "first = This is a test string, sec ="

$string =~ /((.+)*)/;
print "first = $1, sec = $2\n";  #prints "first = This is a test string, sec = This is a test string"

推荐答案

在第一个正则表达式 .* 中匹配了两次.第一次匹配整个字符串.第二次匹配末尾的空字符串,因为 .* 在没有其他匹配的情况下匹配空字符串.

In the first regex .* is matched two times. The first time it matches the whole string. The second time it matches the empty string at the end, because .* matches the empty string when there is nothing else to match.

其他正则表达式不会发生这种情况,因为 .+ 无法匹配空字符串.

This does not happen with the other regex because .+ can't match the empty string.

至于什么去哪里: $2 将包含上次应用 .*/.+ 时匹配的内容.$1 将包含与 (.*)*/(.+)* 匹配的内容,即整个字符串.

As to what goes where: $2 will contain what is matched the last time .* / .+ are applied. $1 will contain what is matched by (.*)* / (.+)*, i.e. the whole string.

这篇关于为什么 .* 不使用这个 Perl 正则表达式中的整个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆