Ratna_Ranjan has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

#!/usr/bin/perl use strict; my $string='Apropovir,"Atripla(tenofovir+emtricitabine+ efavirenz)",'; my @tokens=split(/,\"/,$string);#split using comma&quotes for my $each(@tokens) { $each=~s/\"//;#match quotes and substitute with nothing print "$each\n"; }

output

Apropovir Atripla (tenofovir + emtricitabine + efavirenz),

what i want is how do i match two or more characters such as the comma and ending quotes and substitute with nothing so that i get an output without a comma in the second string??

Apropovir Atripla (tenofovir + emtricitabine + efavirenz)

Replies are listed 'Best First'.
Re: matching two or more characters and substituting
by ikegami (Patriarch) on Nov 06, 2009 at 21:05 UTC

    Direct answer:

    s/[",]//g

    But that's a poor solution. Splitting and dequoting are two separate operations which work on different inputs. It doesn't make much sense to combine them. A better solution:

    #!/usr/bin/perl use strict; use warnings; my $string = 'Apropovir,"Atripla(tenofovir+emtricitabine+ efavirenz)", +'; my @tokens = split(/,/, $string); s/^"//, s/"$// for @tokens; for my $token (@tokens) { print "$token\n"; }

    But the splitting is still problematic. What if there's a comma in the quotes? You should be using Text::CSV_XS.

Re: matching two or more characters and substituting
by keszler (Priest) on Nov 06, 2009 at 21:11 UTC
    my @tokens=split(/[,"]+/,$string);#split using comma&quotes # ^ ^^
    Better: Text::CSV