Why print() in Lua behaving like This? - lua

On The Lua Interpreter
>print("This is a string
>>spread over multiline")
stdin:1: unfinished string near '"This is a'
Since we know on the Lua interpreter we can finish a statement over mulitline
For eg
>a=2
>a=a+
>>1
This works perfectly
Again:
>print([[This is a multiline
>>string]])
This is a multiline
string
This works fine!! then why display error in the first print() statement??

Read the fine Reference Manual:
3.1 – Lexical Conventions
[…]
A short literal string can be delimited by matching single or double
quotes, and can contain the following C-like escape sequences: '\a' (bell),
'\b' (backspace), '\f' (form feed), '\n' (newline), '\r' (carriage
return), '\t' (horizontal tab), '\v' (vertical tab), '\\' (backslash),
'\"' (quotation mark [double quote]), and '\'' (apostrophe [single
quote]). A backslash followed by a line break results in a newline in the
string. The escape sequence '\z' skips the following span of white-space
characters, including line breaks; it is particularly useful to break and
indent a long literal string into multiple lines without adding the newlines
and spaces into the string contents. A short literal string cannot contain
unescaped line breaks nor escapes not forming a valid escape sequence.
[…]
Literal strings can also be defined using a long format enclosed by long
brackets. We define an opening long bracket of level n as an opening
square bracket followed by n equal signs followed by another opening
square bracket. So, an opening long bracket of level 0 is written as [[,
an opening long bracket of level 1 is written as [=[, and so on. A
closing long bracket is defined similarly; for instance, a closing long
bracket of level 4 is written as ]====]. A long literal starts with an
opening long bracket of any level and ends at the first closing long
bracket of the same level. It can contain any text except a closing
bracket of the same level. Literals in this bracketed form can run for
several lines, do not interpret any escape sequences, and ignore long
brackets of any other level. Any kind of end-of-line sequence (carriage
return, newline, carriage return followed by newline, or newline followed
by carriage return) is converted to a simple newline.

Related

How would I put “\” in a string without the apostrophe in the front of it being cancelled out?

For example, if I were to do this:
print(“\”)
It would say: `unfinished string near: ‘“”’
instead of my expected output of: ‘\’
How would I print this? I have searched on google yet still have yet to find an answer.
The backslash (\) is escaping the following character, being the double quote ("), causing the string to be unfinished.
To include an actual backslash in your string, you escape it with another backslash:
print("\\")
From Lua 5.4 Reference Manual, §3.1 (emphasis mine):
A short literal string can be delimited by matching single or double quotes, and can contain the following C-like escape sequences: '\a' (bell), '\b' (backspace), '\f' (form feed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\v' (vertical tab), '\\' (backslash), '"' (quotation mark [double quote]), and ''' (apostrophe [single quote]). [...]

How to escape brackets in a multi-line string in Lua

This is an exercise question from the book the lua programming language the 3th edition.
Exercise2.4: How can you embed the following piece of XML as a string in Lua?
Show at least two different ways.
Here is my answer:
s = "<![CDATA\n Hello world\n]]>"
print(s)
s2 = [[
<![CDATA
Hello world
\]\]>
]]
print(s2)
and the output:
<![CDATA
Hello world
]]>
<![CDATA
Hello world
\]\]>
Way 1 is right. The output of way 2 is not as expected. Without the backslash char, lua will show an error:
lua: execrcise-4.1.lua:7: unexpected symbol near ']'
So I have a question, how to escape brackets in a multi-line string in Lua ?
My lua interpreter version is 5.4.2.
Actually the whole point of this exercise is that you find out how to solve this problem.
Ideally by reading the Lua manual.
There you'll learn that opening and closing brackets for long strings have levels.
Literal strings can also be defined using a long format enclosed by
long brackets. We define an opening long bracket of level n as an
opening square bracket followed by n equal signs followed by another
opening square bracket. So, an opening long bracket of level 0 is
written as [[, an opening long bracket of level 1 is written as [=[,
and so on. A closing long bracket is defined similarly; for instance,
a closing long bracket of level 4 is written as ]====]. A long literal
starts with an opening long bracket of any level and ends at the first
closing long bracket of the same level. It can contain any text except
a closing bracket of the same level.
s2 = [[
<![CDATA
Hello world
]]>
]]
violates that bold rule as you close the long string prematurely leaving you with two extra brackets that cause a syntax error.
So what do you need to do if the string may not contain a closing bracket of level 0 ]] ? We increase the level of our long string.
s2 = [=[
<![CDATA
Hello world
]]>
]=]
You cannot escape a square bracket with a backslash in a Lua string btw.
The only reason why you didn't get an error for the invalid escape sequence \] is that long strings ignore escape sequences.

Convert string to multiline/raw string in LUA

Is there a way to convert a quoted string to a multiline string?
Something like "This string \66 here" to [[This string \66 here]] since I would like to ignore the interpretation of escaped characters.
Lua 5.3 Reference Manual 3.1: Lexical Conventions
Literal strings can also be defined using a long format enclosed by
long brackets. We define an opening long bracket of level n as an
opening square bracket followed by n equal signs followed by another
opening square bracket. So, an opening long bracket of level 0 is
written as [[, an opening long bracket of level 1 is written as [=[,
and so on. A closing long bracket is defined similarly; for instance,
a closing long bracket of level 4 is written as ]====]. A long literal
starts with an opening long bracket of any level and ends at the first
closing long bracket of the same level. It can contain any text except
a closing bracket of the same level. Literals in this bracketed form
can run for several lines, do not interpret any escape sequences, and
ignore long brackets of any other level. Any kind of end-of-line
sequence (carriage return, newline, carriage return followed by
newline, or newline followed by carriage return) is converted to a
simple newline.
For convenience, when the opening long bracket is immediately followed
by a newline, the newline is not included in the string.
That's all you need to know about long strings.
It does not make much sense to convert a string that has been defined using quotes "some string" to a string like [[some string]] as both quotes and square brackets are not actually part of that string and the string itself is the same.
The only difference would be a leading newline which is ignored in square brackets or escape sequences which are not interpreted.
Quotes and square brackets are only part of the string if you have nested strings. In this case conversion also doesn't make much sense because you cannot nest strings with quotes like strings with brackets.
Maybe your whole approach is a bit off?
Do you look for something like this?
local db = "google"
local tbl = "accounts"
local where = "field = 'VALUE' AND TRUE"
local order = "id DESC"
local query = string.format([[
SELECT *
FROM `%s`.`%s`
WHERE %s
ORDER BY %s
]], db, tbl, where, order)

Rails 5 - regex - for string not found [duplicate]

I have following regex handy to match all the lines containing console.log() or alert() function in any javascript file opened in the editor supporting PCRE.
^.*\b(console\.log|alert)\b.*$
But I encounter many files containing window.alert() lines for alerting important messages, I don't want to remove/replace them.
So the question how to regex-match (single line regex without need to run frequently) all the lines containing console.log() and alert() but not containing word window. Also how to escape round brackets(parenthesis) which are unescapable by \, to make them part of string literal ?
I tried following regex but in vain:
^.*\b(console\.log|alert)((?!window).)*\b.*$
You should use a negative lookhead, like this:
^(?!.*window\.).*\b(console\.log|alert)\b.*$
The negative lookhead will assert that it is impossible to match if the string window. is present.
Regex Demo
As for the parenthesis, you can escape them with backslashes, but because you have a word boundary character, it will not match if you put the escaped parenthesis, because they are not word characters.
The metacharacter \b is an anchor like the caret and the dollar sign.
It matches at a position that is called a "word boundary". This match
is zero-length.
There are three different positions that qualify as word boundaries:
Before the first character in the string, if the first character is a
word character.
After the last character in the string, if the last
character is a word character.
Between two characters in the string,
where one is a word character and the other is not a word character.

sublime text replace and find expression regular expression

I want to replace expressions of the form
\theta^\top x_2 +x_1 or \theta^\top x_{2} +x_{1} or \theta^{\top} x_{2} +x_{1} or \theta^{\top}x_{2}+x_{1}
or any other combinations of the same latex math command
by
\theta \diamond x
using sublime text. Is there a way to do this in sublime text using regular expression? I couldn't find the regex for { or }.
That's because { and } are part of the regex syntax. You can escape them by using a slash (\). Something like this might work:
\\theta\^\{?\\top\}?\s*\{?x_\{?2\}?\s*\+\s*x_\{?1\}?
Let's break it down.
\\theta\^ will match the string \theta. Note that we have to escape the \t and ^ since \t is a tab and ^ is part of the regex syntax.
\{? and \}? will match { or } either once or not at all. Again we have to escape the brackets. The question mark (?) means this character can occur 0 or 1 time(s).
\\top - again, we have to escape the tab character \t.
\s* - a space character (\s) zero of more times.
x_\{?2\}?\s*\+\s*x_\{?1\}? will match x_, optional curly brackets with a 2, zero or more spaces followed by a + (which is also part of the regex syntax, so we must escape it), and then x_ with a one (and also optional curly brackets).
Hopefully this clears things up!

Resources