in reply to regular expression (search and destroy)

Here are my 2 cents. This code would do what you are asking, but wouldn't handle other problems like a " in the quoted string.

#!/usr/bin/perl -w my $line = '121212, "Simpson, Bart", Springfield<br>'; print "Before: $line\n"; $line =~ s/"(.*?),(.*?)"/$1_$2/; print "After: $line\n";

The output is:

Before: 121212, "Simpson, Bart", Springfield<br> After: 121212, Simpson_ Bart, Springfield<br>

To explain, adding a ? after *, as in .*? causes minimal matching. It will match the first ", then as few characters as possible, then a comma, then more of the same until another " is found. It all gets replaced with the s///.

Replies are listed 'Best First'.
Re: Re: regular expression (search and destroy)
by sauoq (Abbot) on Nov 12, 2003 at 21:42 UTC
    This code would do what you are asking

    It wouldn't really do what he is asking. It would just work on the example he gave. Consider other possible input... A CSV format generally defines an espape character, often either a doublequote or a backwack. There may be more than one quoted field. Etc. So, what would your code do to a line like:

    42, "Simpson, Homer ""Mr. Donuts"", "Springfield"
    And how would you fix it?

    -sauoq
    "My two cents aren't worth a dime.";