How does split work here?
$string = 'a=1;b=2';
use Data::Dumper;
@array = split("; ?", $string);
print Dumper(\@array);
output:
$VAR1 = [
开发者_如何学JAVA 'a=1',
'b=2'
];
Anyone knows how "; ?"
work here?It's not regex, but works quite like regex,so I don't understand.
I think it means "semicolon followed by optional space (just one or zero)".
It's not regex, but works quite like regex,so I don't understand.
The pattern parameter to split is always treated as a regular expression (would be better to not use a string, though). The only exception is the "single space", which is taken to mean "split on whitespace"
The first parameter of split
is a regex. So I'd rather write split /; ?/, $string;
.
When you use a string for the first parameter, it just means the regex can vary and has to be compiled anew each time the split is run. See perldoc -f split
for details.
The regex could be read; the character ";" optionally followed by a space. See perlretut and perlreref for details.
A semicolon (the ;) followed by an optional (the ?) space (the ).
精彩评论