Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to remove double quotes

Reply
Thread Tools

How to remove double quotes

 
 
Vittal
Guest
Posts: n/a
 
      03-21-2005
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
 
Reply With Quote
 
 
 
 
David K. Wall
Guest
Posts: n/a
 
      03-21-2005
Vittal <(E-Mail Removed)> 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 " */

}
 
Reply With Quote
 
 
 
 
Ted Zlatanov
Guest
Posts: n/a
 
      03-21-2005
On Mon, 21 Mar 2005, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

Vittal <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
regex to avoid escaped quotes or double quotes jOhn Ruby 1 01-29-2008 08:31 PM
PHP double quotes inside double quotes MSB Computer Support 11 10-21-2006 01:09 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Quotes/Double Quotes in Image Control Chris White ASP .Net 1 09-22-2004 06:22 AM



Advertisments