Regular Expression newbie help/question

Discussion in 'Perl Misc' started by Sven, Oct 23, 2003.

  1. Sven

    Sven Guest

    Hi,
    I need some assistance on a search and replace issue with a
    commaseparated file.
    I need to replace all commas(,) on every lines EXCEPT those that occur
    within doublequotes (",")

    Thanks,

    Sven
     
    Sven, Oct 23, 2003
    #1
    1. Advertising

  2. Sven

    Greg Bacon Guest

    In article <>,
    Sven <> wrote:

    : I need some assistance on a search and replace issue with a
    : commaseparated file.
    : I need to replace all commas(,) on every lines EXCEPT those that occur
    : within doublequotes (",")

    If you want to roll your own:

    $ cat try
    #! /usr/local/bin/perl

    use warnings;
    use strict;

    while (<DATA>) {
    s/(".*?"|,)/$1 eq ',' ? '<=>' : $1/eg;
    print;
    }

    __DATA__
    "foo,bar"
    foo,bar
    foo,bar,"baz,quux"
    foo,,bar,"baz,quux"
    foo,bar,,"baz,,quux"

    $ ./try
    "foo,bar"
    foo<=>bar
    foo<=>bar<=>"baz,quux"
    foo<=><=>bar<=>"baz,quux"
    foo<=>bar<=><=>"baz,,quux"

    Consider using Text::CSV_XS, available on the CPAN.

    Greg
    --
    The greatest dangers to liberty lurk in insidious encroachment by men
    of zeal, well-meaning but without understanding.
    -- Justice Louis D. Brandeis
     
    Greg Bacon, Oct 23, 2003
    #2
    1. Advertising

  3. Sven <> wrote:

    > I need to replace all commas(,) on every lines EXCEPT those that occur

    ^^^^^^
    > within doublequotes (",")



    perldoc -q EXCEPT

    How can I split a [character] delimited string except when inside
    [character]? (Comma-separated files)


    You are expected to check the Perl FAQ before posting to the
    Perl newsgroup.

    After you get the fields split, use join() to put them back
    together with some other separator.

    Or, better yet, use a module that understands the CSV format.

    http://search.cpan.org/search?query=CSV&mode=all


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 23, 2003
    #3
  4. Sven wrote:
    > I need some assistance on a search and replace issue with a
    > commaseparated file.


    > I need to replace all commas(,) on every lines EXCEPT those that
    > occur within doublequotes (",")


    s/("[^"]*")|,/$1 ? $1 : $replacement/eg;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Oct 23, 2003
    #4
  5. (Sven) wrote in news:ad7279fb.0310231202.3fcc779
    @posting.google.com:

    > Hi,
    > I need some assistance on a search and replace issue with a
    > commaseparated file.
    > I need to replace all commas(,) on every lines EXCEPT those that occur
    > within doublequotes (",")


    Please read the FAQ before posting. Thanks.

    --
    Eric
    $_ = reverse sort $ /. r , qw p ekca lre uJ reh
    ts p , map $ _. $ " , qw e p h tona e and print
     
    Eric J. Roode, Oct 24, 2003
    #5
  6. Sven

    Helgi Briem Guest

    On Fri, 24 Oct 2003 08:46:37 -0500, "Eric J. Roode"
    <> wrote:

    > (Sven) wrote in news:ad7279fb.0310231202.3fcc779
    >@posting.google.com:
    >
    >> Hi,
    >> I need some assistance on a search and replace issue with a
    >> commaseparated file.
    >> I need to replace all commas(,) on every lines EXCEPT those that occur
    >> within doublequotes (",")

    >
    >Please read the FAQ before posting. Thanks.


    While I will usually be the first one to recommend
    reading the documentation, the least you could do
    is point the OP to the relevant bit of documentation
    as it is far from easy to find.

    "How can I split a [character] delimited string except
    when inside [character]?"

    The relevant section is in perlfaq4 and can be read
    either by browsing through that document:

    perldoc perlfaq4

    or, searching for it within the perlfaq documents:

    perldoc -q split

    or

    perldoc -q delimited
     
    Helgi Briem, Oct 24, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,350
  2. Lee
    Replies:
    6
    Views:
    365
    Alan Moore
    Oct 14, 2003
  3. googleboy
    Replies:
    6
    Views:
    346
    Paul McGuire
    Sep 23, 2005
  4. Peter Vanderhaden
    Replies:
    5
    Views:
    140
    SpringFlowers AutumnMoon
    Sep 30, 2007
  5. Replies:
    10
    Views:
    148
    Tad McClellan
    Nov 12, 2007
Loading...

Share This Page