tags:

views:

78

answers:

1

I have the following data in a CSV file.

2454,"dum my"
12345,"dummy"
300001,"du m m y"

I was wondering, what regular expression I can use, to turn them into

002454,"dummy"
012345,"dummy"
300001,"dummy"

Thanks.

+3  A: 

I'd recommend reading and rewriting the CSV file with Text::CSV_XS and manipulating the fields individually, but if you must just use a regex, something like:

sub strip_spaces { my $str = shift; $str =~ y/ //d; $str }
s/(\d+)/ sprintf '%.6d', $1 /e;
s/("[^"]+"$)/ strip_spaces($1) /e;
ysth
May I know what is 'y' within strip_space? as I always saw 'm' and 's', but this is the first time I saw 'y'.
Yan Cheng CHEOK
@Yan: see [perldoc perlop](http://perldoc.perl.org/perlop.html) under `tr///`.
Ether
Legal link: http://perldoc.perl.org/perlop.html#tr/SEARCHLIST/REPLACEMENTLIST/cds
ysth
@Sinan Sorry. I don't realize that was unauthorized copies. I remove it then :)
Yan Cheng CHEOK
I once don't like Perl due to its difficult used symbol. Now I can appreciated it and understand why it is being called "Swiss Army chainsaw". I am able to solve my problem with just 12 lines of codes! Thanks.
Yan Cheng CHEOK