Hello everyone,
I am trying to isolate numbers from string, manipulate the numbers then put them back to original positions in the string. When I try to use zero-width separator, the split function looks weird, it generated much more elements than I expect.
Are there any mistakes? Why the split behaviors like this? (The regexp looks right when I used it s///)
Could anyone help?
Thanks,
Tiger
#build the seperator
:-) perl -le '$a = "15678.91 ml; r45.12 ";
$a =~ s/((?!(\d|\.))(?<=(\d|\.)))|((?=(\d|\.))(?<!(\d|\.)))/|/g;
print $a'
|15678.91| ml; r|45.12|
|15678.91| ml; r|45.12|
#use the seperator in split
:-) perl -MData::Dumper -le '$a = "15678.91 ml; r45.12 ";
@a = split /((?!(\d|\.))(?<=(\d|\.)))|((?=(\d|\.))(?<!(\d|\.)))/, $a;
print Dumper(@a)'
$VAR1 = '15678.91';
$VAR2 = '';
$VAR3 = undef;
$VAR4 = '1';
$VAR5 = undef;
$VAR6 = undef;
$VAR7 = undef;
$VAR8 = ' ml; r';
$VAR9 = undef;
$VAR10 = undef;
$VAR11 = undef;
$VAR12 = '';
$VAR13 = '4';
$VAR14 = undef;
$VAR15 = '45.12';
$VAR16 = '';
$VAR17 = undef;
$VAR18 = '2';
$VAR19 = undef;
$VAR20 = undef;
$VAR21 = undef;
$VAR22 = ' ';
$VAR1 = '15678.91';
$VAR2 = '';
$VAR3 = undef;
$VAR4 = '1';
$VAR5 = undef;
$VAR6 = undef;
$VAR7 = undef;
$VAR8 = ' ml; r';
$VAR9 = undef;
$VAR10 = undef;
$VAR11 = undef;
$VAR12 = '';
$VAR13 = '4';
$VAR14 = undef;
$VAR15 = '45.12';
$VAR16 = '';
$VAR17 = undef;
$VAR18 = '2';
$VAR19 = undef;
$VAR20 = undef;
$VAR21 = undef;
$VAR22 = ' ';
没有评论:
发表评论