加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl Learning - 16 (split, join, list m//, +? *? {}? ??)

发布时间:2020-12-15 21:02:15 所属栏目:大数据 来源:网络整理
导读:split can create list from given string,the format is: @fields=split /separtor/,$string; ? The 'separtor' usually are : or space,and it can be any other charactar/symbol. ? @fields=split /:/,":abc:def::g:h::"; foreach(@fields){ ?print "$_
split can create list from given string,the format is:
@fields=split /separtor/,$string;
?
The 'separtor' usually are : or space,and it can be any other charactar/symbol.
?
@fields=split /:/,":abc:def::g:h::";
foreach(@fields){
?print "$_n";
}
?
By default split keeps the 'undef(s)' at the beginning and drops those at the end,into a list.
To keep those at the end,use -1 as the last argument of split.
?
@fields=split /:/,":abc:def::g:h::",-1;
foreach(@fields){
?print "$_n";
}
?
split defaultly handle $_ if no string provided:
?
$_="? This?? is a t ???test.n";
my @args=split /s+/;
foreach(@args){
?print "$_n";
}
?
If non patten is provided,it takes /s+/ by default,so the same as blow:
?
$_="? This?? is a t ???test.n";
my @args=split;
foreach(@args){
?print "$_n";
}
?
join do the oppsite thing from split,it join elements of list into a string.
Format: $string=join $glue,@pieces;
?
my $x=join ":",4,6,8,10,12;
print "$xn";
?
If the list has only one element,join does nothing; join a empty list gets an empty.
?
my $y=join " foo ","bar";
print "$yn";
my @empty;
my $empty=join "baz",@empty;
print "$emptyn";
?
We can split a string into pieces and them join them with other glues:
?
my @values=split /:/,$x;
my $z=join "-",@values;
?
Note split gets a list,join gets a string; join use character as glue,but not /patten/
?
m// in list context returns the matches list.
?
$_="Hello there,neighbor!";
my ($first,$second,$third)=/(S+) (S+),(S+)/;
print "$second is my $thirdn";
?
/i /g /s can also be used.
?
my $text="Fred dropped a 5 ton granite block on Mr. Slate";
my @words=($text=~/[a-z]+/ig);
print "Result: @wordsn";
#Result: Fred dropped a ton granite block on Mr slate
?
By default,* + {m,n} are all greedy,which tries to match as much as it can.
We can use their non-greedy mod to just match the least characters.
?
$_="fred and barney went bowling last night,barney won";
if(/(?<names>fred.+barney)/){
?print "$+{names}n";
?# fred and barney went bowling last night,barney
}
?
$_="fred and barney went bowling last night,barney won";
if(/(?<names>fred.+?barney)/){
?print "$+{names}n";
?fred and barney
}
?
The greedy + works like this:
When it matches fred,going to .+ it eats all the rest of characters except n at the end. It searches barney from the end,character by character,if not found it throws out one character then search again; if found then match the whole patten,returns.
?
The non-greedy +? works like this:
When it matches fred,going to .+? it eats the next one character and search barney,if not match eats one more character and search again,until it matches barney it stops eating and returns.
All the non-greedy guys are:
+?*?{}???

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读