Match inner quotes ?

A

Abhinav

Hi,

I posted this on comp.editors earlier, so I apologize for this multi-post.
However, I thought it would be great if I knew how to perform the following
using Perl :

I have a file with a number of lines like this :

push @xmlParams,"<?xml version="1.0"?>";
^ ^
^ ^
I need to escape the inner quotes i.e, change " to \" - however number of
times the quotes may appear.
(They will always be balanced, though).

Any pointers on how to do this ?

TIA
 
A

Abhinav

Abhinav said:
Hi,

I posted this on comp.editors earlier, so I apologize for this multi-post.
However, I thought it would be great if I knew how to perform the following
using Perl :

I have a file with a number of lines like this :

push @xmlParams,"<?xml version="1.0"?>";
^ ^
^ ^
I need to escape the inner quotes i.e, change " to \" - however number of
times the quotes may appear.
(They will always be balanced, though).

Any pointers on how to do this ?

Backing off to a higher level, What I /really/wanted to do was to take an
xml file and for each line in it, convert it into the Perl statement. So, I
originally had a file called template.xml which had this :

<?xml version="1.0"?>
....

I was changing it to the Perl statement using :

perl -npi -e 's/(.+)(\n*)/ push \@xmlParams,"${1}";${2}/g' template.xml

However, after the previous post, I changed this to

perl -npi -e 's/"/\\"/g; s/(.+)(\n*)/ push \@xmlParams,"${1}";${2}/g'
template.xml

So that I already have the "s escaped properly after the 1st statement.

Ugly, but works .. Would appreciate Other Ways to do it .. especially in a
single regexp, as my OP required :)

TIA
 
E

Eugene Mikheyev

I think he means the following:
"........"........."............"........"........."........".........."
|........1.........1...........2.......2........3.......3..........|
outer outer

But this is going to be very simple
 
G

gnari

Abhinav said:
perl -npi -e 's/"/\\"/g; s/(.+)(\n*)/ push \@xmlParams,"${1}";${2}/g'
template.xml

So that I already have the "s escaped properly after the 1st statement.

Ugly, but works .. Would appreciate Other Ways to do it .. especially in a
single regexp, as my OP required :)

why not: (untested)
perl -pi -e 's/(.+)/ push \@xmlParams,qq(${1});/' template.xml

gnari
 
A

Anno Siegel

Abhinav said:
Hi,

I posted this on comp.editors earlier, so I apologize for this multi-post.
However, I thought it would be great if I knew how to perform the following
using Perl :

I have a file with a number of lines like this :

push @xmlParams,"<?xml version="1.0"?>";
^ ^
^ ^
I need to escape the inner quotes i.e, change " to \" - however number of
times the quotes may appear.
(They will always be balanced, though).

Any pointers on how to do this ?

As Abigail has pointed out, the nesting of quotes is ambiguous.
If we re-state the problem as "escape all quotes in a string except
the first and the last one", here is one way:

substr( $x, $+[ 1], $-[ 2] - $+[ 1]) =~ s/"/\\"/g if $x =~ /(").*(")/;

Anno
 
A

Abhinav

Anno said:
As Abigail has pointed out, the nesting of quotes is ambiguous.

Indeed :( Will take care to be accurate and comprehensible in the future.
If we re-state the problem as "escape all quotes in a string except
the first and the last one", here is one way:

substr( $x, $+[ 1], $-[ 2] - $+[ 1]) =~ s/"/\\"/g if $x =~ /(").*(")/;

Thanks ! Although my problem was solved (and Gnari mentioned the qq
operator which I should have used in the the first place), this will give
me a lot to learn during the coming weeknd :)
 

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

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top