I'm learning Lua and I know that it is possible to create a multiline string using [[
and ]]
, that is, it is possible to do this:
html = [[
<body>
<head>
</head>
<body>
</body>
</body>
]]
print(html)
The result would be:
<body>
<head>
</head>
<body>
</body>
</body>
Now I want the same thing with an example using XML and CDATA , since for the purposes of my tests I am trying to mix the brackets. I have defined this in my script:
xml = [[
<![CDATA[
<greeting>
Hello, world!
</greeting>
]]>
]]
print(xml)
But when I run it, I get the following error:
lua: test.lua:7: unexpected symbol near ']'
It seems you are mistaking the square brackets on line 6 as the end of my string. How can I make it work?
According to the documentation on Strings in Lua , in the section Multiline quotes , subsection Nesting quotes , it is explained that when you need to have brackets inside a multiline string, you must add a
=
between the start and end brackets of your string for each pair of brackets[]
that will be found within your chain. Since youCDATA
use 2 pairs of square brackets (as shown)You must use two symbols
=
. That is, you must use[==[
and]==]
to define your text string that contains brackets. Your code would be like this:Here is an example of your script running on ideone: http://ideone.com/y6YdfK