perl regex : surround tabbed numeric field by double quotes

Discussion in 'Perl Misc' started by toralf, Sep 9, 2009.

  1. toralf

    toralf Guest

    I've a file containing tab separated values - most, but not all are
    quoted - and now I'm wondering how to substitute a non-quoted value like
    <tab>20090807<tab> by sth. like <tab>"20090807"<tab>

    --
    MfG/Sincerely

    Toralf Förster
    pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3
     
    toralf, Sep 9, 2009
    #1
    1. Advertising

  2. toralf

    Guest

    On Wed, 9 Sep 2009 14:39:59 +0000 (UTC), toralf <> wrote:

    >I've a file containing tab separated values - most, but not all are
    >quoted - and now I'm wondering how to substitute a non-quoted value like
    ><tab>20090807<tab> by sth. like <tab>"20090807"<tab>


    You would have to do some complicated regex (possibly multiple regx's),
    but that depends on the data sample in regards to quote's, newlines, or other
    shapes it can have.
    A simple way is to split on tab, fix up the value, the join it back together.
    Its ugly though.

    -sln
    ------------
    use strict;
    use warnings;

    my @ar = (
    qq{\n12345145\t\n36367\t"qfqqbv"\n\t"0987"\t"asdf"a"\n },
    qq{\t"01234"\taaaa\t494848\t}
    );

    for my $str (@ar) {
    my $newstring = join "<tab>", map {/^(\s*|)"*(.*?|)"*(\s*|)$/; $1.'"'.$2.'"'.$3 } split (/\t/,$str);
    print "-> $newstring\n\n";
    }
    __END__
     
    , Sep 9, 2009
    #2
    1. Advertising

  3. toralf

    Guest

    On Wed, 09 Sep 2009 17:19:50 -0700, David Harmon <> wrote:

    >On Wed, 09 Sep 2009 12:09:05 -0700 in comp.lang.perl.misc,
    > wrote,
    >
    >> (\s*|)

    >
    >Just in case \s* fails to match the empty string, eh?


    Doh, what was I thinking.
    /^(\s*)"*(.*?)"*(\s*)$/

    -sln
     
    , Sep 10, 2009
    #3
  4. toralf

    Guest

    On Wed, 09 Sep 2009 21:47:52 -0500, Tad J McClellan <> wrote:

    > <> wrote:
    >
    >> Doh, what was I thinking.
    >> /^(\s*)"*(.*?)"*(\s*)$/

    >
    >
    >Errr, that matches *every* string...


    Go figure, thats what I want...

    my $newstring = join "<tab>", map {/^(\s*|)"*(.*?|)"*(\s*|)$/; $1.'"'.$2.'"'.$3 } split (/\t/,$str);

    -sln
     
    , Sep 10, 2009
    #4
  5. toralf

    Guest

    On Wed, 09 Sep 2009 20:55:26 -0700, wrote:

    >On Wed, 09 Sep 2009 21:47:52 -0500, Tad J McClellan <> wrote:
    >
    >> <> wrote:
    >>
    >>> Doh, what was I thinking.
    >>> /^(\s*)"*(.*?)"*(\s*)$/

    >>
    >>
    >>Errr, that matches *every* string...

    >
    >Go figure, thats what I want...
    >
    >my $newstring = join "<tab>", map {/^(\s*|)"*(.*?|)"*(\s*|)$/; $1.'"'.$2.'"'.$3 } split (/\t/,$str);

    /^(\s*)"*(.*?)"*(\s*)$/
    Its not perfect, but it's a start for somebody.

    -sln
     
    , Sep 10, 2009
    #5
  6. toralf

    toralf Guest

    toralf wrote:

    > I've a file containing tab separated values - most, but not all are
    > quoted - and now I'm wondering how to substitute a non-quoted value like
    > <tab>20090807<tab> by sth. like <tab>"20090807"<tab>


    Thx all - this works for me :

    $s =~ s/\t\d{8}\t/\t"$&"\t/;

    --
    MfG/Sincerely

    Toralf Förster
    pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3
     
    toralf, Sep 14, 2009
    #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. Chris White

    Quotes/Double Quotes in Image Control

    Chris White, Sep 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,855
    Hermit Dave
    Sep 22, 2004
  2. Bruce W...1
    Replies:
    4
    Views:
    3,055
    Sid Ismail
    Oct 19, 2003
  3. Sydex
    Replies:
    12
    Views:
    6,508
    Victor Bazarov
    Feb 17, 2005
  4. .rhavin grobert

    how to surround a constant with quotes?

    .rhavin grobert, Mar 13, 2007, in forum: C Programming
    Replies:
    5
    Views:
    346
    Old Wolf
    Mar 13, 2007
  5. jOhn
    Replies:
    1
    Views:
    227
    Phlip
    Jan 29, 2008
Loading...

Share This Page