How to remove double quotes

Discussion in 'Perl Misc' started by Vittal, Mar 21, 2005.

  1. Vittal

    Vittal Guest

    Hello All,

    I am trying to write a perl script which removes the contents between
    two plain double quotes. (I am parsing C and C++ files.) What I mean
    by plain is, double quote (") should not have been preceeded by single
    quote (') or back slash(\).

    For this I wrote the following code, which is serving the purpose but
    partially.

    *******************************************************************************
    #!/usr/bin/perl
    open FILE , "./y.c";
    while (<FILE>)
    {
    $temp .= $_;
    }

    $temp1 = $temp;

    while ($temp && $temp =~ /[^'\\]("([^"])*")/s)
    {
    $find = $1;
    $temp = $';
    if (!($2 =~ /['\\]/)){
    $temp1 =~ s/\Q$find//;
    }
    }

    print $temp1;
    ******************************************************************************

    So if I have a .c file like this:
    ******************************************************************************
    #include <stdio.h>

    int main ()
    {
    char c = '"';
    printf("I should be removed \n");
    printf ("Testing under proggress
    go ahead \n");

    /* "this should be \" removed" I should be here "but kill me here " */
    }
    *****************************************************************************
    output should look like
    **********************************************************************
    #include <stdio.h>

    int main ()
    {
    char c = '"';
    printf();
    printf ();

    /* I should be here */
    }
    ******************************************************************************
    but with my version of perl script I get the output something like
    this:
    ******************************************************************************
    #include <stdio.h>

    int main ()
    {
    char c = '"';
    printf();
    printf ();

    /* "this should be \" removedbut kill me here " */
    ****************************************************************************

    Can someone help me to get this regular expression correct??

    Thanks
    -Vittal
    Vittal, Mar 21, 2005
    #1
    1. Advertising

  2. Vittal <> wrote:

    > I am trying to write a perl script which removes the contents
    > between two plain double quotes. (I am parsing C and C++ files.)
    > What I mean by plain is, double quote (") should not have been
    > preceeded by single quote (') or back slash(\).


    The following code does what you asked for, but I'm not convinced
    that it does what you want. I can easily construct text that will
    yield undesirable results.

    Maybe someone more knowledgeable than me will point out a better
    solution.




    use strict;
    use warnings;

    my $text;
    {
    local $/ = undef;
    $text = <DATA>;
    }

    $text =~ s/
    (?<!['\\]) " # " not preceded by ' or \
    .*?
    (?<!['\\]) "
    //gsx;

    print $text;



    __DATA__
    #include <stdio.h>

    int main ()
    {
    char c = '"';
    printf("I should be removed \n");
    printf ("Testing under proggress
    go ahead \n");

    /* "this should be \" removed" I should be here "but kill me here "
    */

    /* more test text...
    "this
    should
    be \" removed"
    I
    should be
    here "but kill
    me here " */

    }
    David K. Wall, Mar 21, 2005
    #2
    1. Advertising

  3. Vittal

    Ted Zlatanov Guest

    On Mon, 21 Mar 2005, wrote:

    Vittal <> wrote:
    >
    >> I am trying to write a perl script which removes the contents
    >> between two plain double quotes. (I am parsing C and C++ files.)
    >> What I mean by plain is, double quote (") should not have been
    >> preceeded by single quote (') or back slash(\).

    >
    > The following code does what you asked for, but I'm not convinced
    > that it does what you want. I can easily construct text that will
    > yield undesirable results.


    The OP could also try the CPAN Text::Balanced module. The requirement
    of a quoting single quote is just weird, but Text::Balanced can
    probably handle it since it lets you specify any number of escape
    characters.

    Ted
    Ted Zlatanov, Mar 21, 2005
    #3
    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,843
    Hermit Dave
    Sep 22, 2004
  2. Chris
    Replies:
    1
    Views:
    13,627
    Oisin
    Mar 24, 2006
  3. Lawrence Tierney

    Multiline quotes - escaping quotes - et al

    Lawrence Tierney, Dec 24, 2003, in forum: Java
    Replies:
    3
    Views:
    4,488
    Andrew Thompson
    Dec 24, 2003
  4. Sydex
    Replies:
    12
    Views:
    6,472
    Victor Bazarov
    Feb 17, 2005
  5. jOhn
    Replies:
    1
    Views:
    219
    Phlip
    Jan 29, 2008
Loading...

Share This Page