Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: regular expression (search and destroy)

by Lhamo Latso (Scribe)
on Nov 12, 2003 at 21:06 UTC ( [id://306637]=note: print w/replies, xml ) Need Help??


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.";
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://306637]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (8)
As of 2024-04-23 22:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found