This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Double quotes inside string

5 posts · 4516 views

Hi, my problem is the next
I recieve an string like this:
"<OMA>
<OMS cd="Computing" name="Homology"/>
<OMA>
<OMS cd="simplicial-group" name="Loop-Space"/>
<OMA>
<OMS cd="Simplicial-Set" name="sphere"/>
<OMI>4</OMI>
</OMA>
<OMI>2</OMI>
</OMA>
<OMI>2</OMI>
</OMA>"
so I have a problem, because if i try to read this string in Common Lisp i get
<OMA>
<OMS cd=
Exists some way to solve this problem, i.e, if i read it i would like to get
<OMA>
<OMS cd="Computing" name="Homology"/>
<OMA>
<OMS cd="simplicial-group" name="Loop-Space"/>
<OMA>
<OMS cd="Simplicial-Set" name="sphere"/>
<OMI>4</OMI>
</OMA>
<OMI>2</OMI>
</OMA>
<OMI>2</OMI>
</OMA>

Re: Double quotes inside string

it'd be easier if you posted a complete snippet of code which shows the problem

..some code that works as expected here:
CL-USER> (with-input-from-string (stream "<OMA><OMS cd=\"Computing\" name=\"Homology\"/></OMA>")
           (read-line stream))

"<OMA><OMS cd=\"Computing\" name=\"Homology\"/></OMA>"
T
CL-USER> (princ *)
<OMA><OMS cd="Computing" name="Homology"/></OMA>
"<OMA><OMS cd=\"Computing\" name=\"Homology\"/></OMA>"
CL-USER> (prin1 *)
"<OMA><OMS cd=\"Computing\" name=\"Homology\"/></OMA>"
"<OMA><OMS cd=\"Computing\" name=\"Homology\"/></OMA>"
CL-USER>

Re: Double quotes inside string

I believe all you need to do here is escape the quotes that are part of your string, with the backslash character ('\')

So, wherever you have something like this:

<OMS cd="Computing" name="Homology"/>

...change it to this:

<OMS cd=\"Computing\" name=\"Homology\"/>

This is common to most programming languages.

Re: Double quotes inside string

just to make sure there is no misunderstanding; one do not have to escape an actual data-source or the file content, if reading from a file:
lnostdal@blackbox:~$ nano -w test.txt
lnostdal@blackbox:~$ cat test.txt
<OMA><OMS cd="Computing" name="Homology"/></OMA>
lnostdal@blackbox:~$ 
CL-USER> (with-open-file (stream "/home/lnostdal/test.txt")
           (read-line stream))
"<OMA><OMS cd=\"Computing\" name=\"Homology\"/></OMA>"
NIL
CL-USER> (princ *)
<OMA><OMS cd="Computing" name="Homology"/></OMA>
"<OMA><OMS cd=\"Computing\" name=\"Homology\"/></OMA>"
CL-USER> 

Re: Double quotes inside string

just to make sure there is no misunderstanding; one do not have to escape an actual data-source or the file content, if reading from a file:
Right, this is because you do not want to use the reader when you just want the string data out of it, you just want read-line (or maybe read-sequence in some cases). The reader does a lot of stuff like parsing the input and converting it into Lisp data, which includes trying to understand double quotes.

And if it is the case that you actually are working with lots of string literals in your code with double quotes in it, a library like cl-interpol (http://www.weitz.de/cl-interpol/) might help (for some well behaved input). It will certainly escape your quotes. Or you could define something like your own text block reader macro similar to the Python """ facility or a "here document".

Zach S