perl split 的一种特殊用法

本文详细解析了Perl编程中split函数的正规语法及其特殊用法,包括如何使用单引号或双引号分隔空格,并通过实例展示了不同参数下split函数的行为,帮助读者更好地理解和应用这一内置函数。

参考 http://blog.chinaunix.net/uid-1919528-id-2792055.html
split 函数的正规语法应该是:

split /PATTERN/, EXPR

而使用单引号(或者双引号)来分隔空格(whitespace)是一种特殊的例子:

As a special case, if the expression is a single space (" "), the function splits on
whitespace just as split with no arguments does. Thus, split(" ") can be used to
emulate awk ’s default behavior. In contrast, split(/ /) will give you as many null
initial fields as there are leading spaces. (Other than this special case, if you supply
a string instead of a regular expression, it’ll be interpreted as a regular expression
anyway.) -- Programming Perl
#!/usr/bin/perl -w

use strict;

my $test = "the  first gap has two whitespace";
$_ = $test;

my $first = (split)[1];
my $second = (split(" ", $test))[1];
my $third = (split(/ /, $test))[1]; # 贪婪匹配,匹配尽量多的空格

print "\$first is $first\n"; # $first is first
print "\$second is $second\n"; # $second is first
print "\$third is $third\n"; # $third is

转载于:https://www.cnblogs.com/liuhui0622/p/4626689.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值