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

Hi all,
I have a string "ab"cde"fgh" and i want to print the substrings as follows,
"ab" "cde" "fgh"
Below is the code i have tried,
perl -e '$_="\"ab\"cde\"fgh\"";print $1,"\t" while /(".*?(?="))/g'
OUTPUT
"ab "cde "fgh

How to perform this using regex match or with split??
Thanks in advance!!

Replies are listed 'Best First'.
Re: Regexp doubt
by CountZero (Bishop) on May 04, 2010 at 07:56 UTC
    my @items = grep {$_} split '"', '"ab"cde"fgh"'; print join '|', @items;
    Output:
    ab|cde|fgh

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Regexp doubt
by jwkrahn (Abbot) on May 04, 2010 at 06:16 UTC
    perl -le '$_ = q["ab"cde"fgh"]; print join "\t", map qq["$_"], /[^"]+/ +g'
Re: Regexp doubt
by Anonymous Monk on May 04, 2010 at 06:01 UTC
    I won't question your doubts, but I will answer your question: s/(?<=.)"(?=.)/" "/g;.
Re: Regexp doubt
by AnomalousMonk (Archbishop) on May 04, 2010 at 08:30 UTC

    Another solution, using overlapping capture groups (uses ' single-quotes in example instead of " double-quotes because of excessive escapology needed by Windoze shell to handle doubles):

    >perl -wMstrict -le "my $s = q{'ab'cde'fgh'}; my @ss = $s =~ m{ (?= (' [^']* ')) }xmsg; print qq{($_)} for @ss; " ('ab') ('cde') ('fgh')
Re: Regexp doubt
by Marshall (Canon) on May 04, 2010 at 11:15 UTC
    #!/usr/bin/perl -w use strict; my $string = '"ab"cde"fgh"'; my @tokens = $string =~ m/\"\w+/g; print "$_\" " foreach @tokens; #prints: "ab" "cde" "fgh"
Re: Regexp doubt
by kiruthika.bkite (Scribe) on May 04, 2010 at 08:34 UTC
    Is this you are expecting ?
    $_='"ab"cde"fgh"'; s/("(.*?)")((.*?)")/$1 "$3 "/; print $_;
      Thanks all for handy solutions.
      Improvised from Anomalous Monk's solution
      perl -e '$_= q["ab"cde"fgh"];print $1,"\t" while m/(?=("[^"]*"))/g'
Re: Regexp doubt
by pavunkumar (Scribe) on May 05, 2010 at 04:25 UTC
    Try this things , it will work fine....
    $str = "\"ab\"cde\"fgh\""; foreach ( split ("\"" , $str ) ) { print "\"$_\" " if length ( $_ ) ; }