>>>>> "AA" == Anirban Adhikary <anirban.adhik...@gmail.com> writes:
AA> my $x = '12abc34bf5'; AA> @num = split /(a|b)+/, $x; AA> print "NUM=@num\n"; AA> NUM=12 b c34 b f5 AA> Does it mean split the string ,here separaters are 'a' or 'b'(one or AA> more occurance because of + metacharacter). AA> If it matches from left i.e. a comes before b in the $x then why I am AA> getting 'b' as the 4th element in @num? this is somewhat tricky and covers several things. this one liner shows the same result. perl -le '($x) = "xabc" =~ /(a|b)+/; print $x' b that is matching the 'a' and because of + then matching the 'b'. but the grab only grabs the last of the alternations to match so it returns 'b'. the alternation is matching but it can't grab more than one of the choices so you get the last one. the same thing is happening in the split. you have parens around the delimiter so the grab will be returned in the list. the same logic happens and 'a' is matched and then 'b' is and 'b' is the final delimiter and that is returned. AA> changing the split statement produces same result why? AA> my @num = split /(b|a)+/, $x AA> print "NUM=@num\n"; AA> OUTPUT: AA> NUM=12 b c34 b f5 AA> also I need the explanation of the output because. I cant understand AA> that if split function splits the string based on separater 'a' then I AA> should get bc34bf5 as second element. it splits on the thing inside the parens which matches 'b' last since it occurs after 'a' in the string. the last thing matched is grabbed and returned. AA> But the non capturing grouping works as fine and I can understand it easily. AA> my $x = '12abc34bf5'; AA> @num = split /(?:a|b)+/, $x; AA> print "NUM=@num\n"; since nothing is grabbed, you get the split you want. the act of grabbing can only grab 'a' OR 'b' but not a string of 'a's and 'b's. you need to use a char class for that like [ab]+. this is how alternation works with grabbing. uri -- Uri Guttman ------ u...@stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com --------- -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/