How to remove double quotes

V

Vittal

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
 
D

David K. Wall

Vittal said:
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 " */

}
 
T

Ted Zlatanov

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top