jjjamie Posted November 10, 2002 Posted November 10, 2002 Hi, Can anyone convert this Perl code to VB.NET? sub parseCSV { # (str) returns array of CSV entries (commas separated fields) # start with Jeffrey E. F. Friedl, "Mastering Regular Expressions" method on p 205 # modified to interpret "" as a quoted " # Correctly parses CSV file produced by Microsoft Excel 97. my $comma=''; my $str = $_[0]; my $preStr=$str; my @fields = (); # initialize to null until ( $str eq '' ) { my $thisField=''; if( $str =~ m{^"([^",]*)"(|,(.*))$} ) { $thisField = $1; $str=$3; $comma=$2; } elsif ( $str =~ m{^([^",]*)(|,(.*))$} ) { $thisField = $1; $str=$3; $comma=$2; } elsif ( $str =~ m{^"(.*)$} ) { # there is a leading " $str=$1; # get all "" in remainder of this field while( $str =~ m{^([^"]*")"(.*)$} ) { $thisField .= $1; $str=$2; }; #hopefully we are at a [^,"]*", or " at end of line if( $str =~ m{([^"]*)"(|,(.*))$} ) { $thisField .= $1; $str=$3; $comma=$2; } else { warn "Could not find a \" following >$thisField< in\n>$preStr<"; $thisField .= $str; $str=''; }; } else { warn "Could not match >$str< in\n>$preStr<"; $str = ''; }; push( @fields, $thisField ); # add the just matched field } push( @fields, undef) if $comma =~ m/,$/; #account for an empty last field return @fields; } # end parseCSV Quote
*Gurus* divil Posted November 11, 2002 *Gurus* Posted November 11, 2002 Sure. I charge £60/hr. Seriously, asking people to convert code for you is a bit much. You're much better off having a go yourself and asking specific questions when you get stuck. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.