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

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

概述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 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 "$x\n";   If the List has only one element,join does nothing; join a empty List gets an empty.   my $y=join " foo ","bar";
print "$y\n"; my @empty;
my $empty=join "baz",@empty;
print "$empty\n";   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 $third\n";   /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: @words\n";
#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: +?*?{}??? 总结

以上是内存溢出为你收集整理的Perl Learning - 16 (split, join, list m//, +? *? {}? ??)全部内容,希望文章能够帮你解决Perl Learning - 16 (split, join, list m//, +? *? {}? ??)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1293053.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存