Lua查找字符串中的操作数
我有一个Lua字符串,如“382+323”或“32x291”或“94-23”,如何检查并返回操作数的位置 我发现Lua查找字符串中的操作数,lua,Lua,我有一个Lua字符串,如“382+323”或“32x291”或“94-23”,如何检查并返回操作数的位置 我发现字符串。查找(“[+x-]”)无效。有什么想法吗 th> str = '5+3' th> string.find(str, '[+-x]') 1 1 th> string.find(str, '[+x-]') 2 2 [+-x]是介于“+”和“x”之间的1个字符的模式匹配。 如果要将破折号用作字符而不是元字符,则应以破折号开始或结束字符组。。显示准确的代码,
字符串。查找(“[+x-]”)无效。有什么想法吗
th> str = '5+3'
th> string.find(str, '[+-x]')
1 1
th> string.find(str, '[+x-]')
2 2
[+-x]是介于“+”和“x”之间的1个字符的模式匹配。
如果要将破折号用作字符而不是元字符,则应以破折号开始或结束字符组。。显示准确的代码,包括测试用例。“[+x-]”似乎有效,但“[+-x]”将返回不同的结果。为什么?”-“是[]
集合中的特殊字符。如果您编写[+-x]
它将被解释为一个字符范围,就像[a-z]
一样,但是如果-
显示为最后一个字符,那么它将被视为一个-
@C.Wang刚刚开始的Lua。在基本lua解释器中,每当我想查看操作结果时,我都必须键入print$(
)。当我使用python/ipthon时,我只需键入变量的名称即可查看其值。您使用的lua解释器是什么?
print("Type an arithmetic expression, such as 382 x 3 / 15")
expr = io.read()
i = -1
while i do
-- Find the next operator, starting from the position of the previous one.
-- The signals + and - are special characters,
-- so you have to use the % char to escape each one.
-- [The find function returns the indices of s where this occurrence starts and ends][1].
-- Here we are obtaining just the start index.
i = expr:find("[%+x%-/]", i+1)
if i then
print("Operator", expr:sub(i, i), "at position", i)
end
end