my_lua_string = [=[ This is a string delimited with long brackets; it contains other valid long brackets like [[this]] (or this: ]==]), but they are ignored because Lua looks for a long bracket that matches the opening delimiter. ]=]
Lua supports something I haven't seen in any other language: so-called 'long brackets', sometimes also referred to as 'long strings' or 'double brackets'. There is an infinite variety of valid long bracket pairs: [[my multiline string]], [=[...]=], [==[...]==], and so on. You can use them for multiline comments like so: --[[...]], --[=[...]=], etc. This means that any string literal can be created, or any code can be commented, without escaping the contents: you just choose a long bracket with a number of equals signs such that the ending delimiter doesn't occur inside the string.
Are there any other languages with infinitely extensible string delimiters? I know about Perl's q-style, but that is AFAIK limited to single characters: q#...#, qx...x, etc. I'm especially interested in solutions which, like Lua's, go beyond single-character delimiters.
(For the curious: this question bubbled up when I tried to ssh multiple commands with ssh 'multiple; commands', where one of those commands was a call to sudo sh -c '...'. I think shell is the only environment where I frequently manually create string literals with multiple levels of quoting.)
For those who want to know more about Lua's long brackets:
- The Lua 5.3 Reference Manual (search for 'long bracket')
- Examples at Lua wiki strings tutorial (search for 'nesting quotes')
- Example at Well House Consulting (also contains a multiline comment example)
A valid Lua comment, and a valid Lua string:
--[[ This comments out an assignment to my_lua_string
my_lua_string = [==[one [=[inner]=] two]==]
]]
-- This is a string delimited with long brackets
[=[one ]] two]=]
--> 'one ]] two'
java.util.Scanner. Too broad. – user207421 Feb 2 at 9:22java.util.Scannerdocs it seems that is for parsing strings, not a syntactic construct to quote string literals. 'Too broad' does not seem to apply: " if your question could be answered by an entire book, or has many valid answers (but no way to determine which - if any - are correct), then it is probably too broad for our format." This question is about a clearly rare syntactic construct; it does not need a book, nor has it got many valid answers. – Esteis Feb 2 at 9:29