分析 Lua 中的字符串

请问有人能告诉我如何从变量中读取文本,例如:

>Hello World
text,1000
text2,200

读取:

  1. 如果某行开头为>,且该行长度不超过50个字符,则读取该行,否则继续读取下一行。 (其中有一部分检查代码如下:
if string.sub(tekst, 1, 1) == '>' then
    ...
end

但其中没有检查行长度的部分)

  1. 下一行 - 将文本按","分离,并先从“,”之前获取文本,然后再从“,”之后获取文本到不同的变量(以便通过脚本的另一部分进行分析)。

非常感谢。

原文链接 https://stackoverflow.com/questions/8906747

点赞
stackoverflow用户513763
stackoverflow用户513763

你不太清楚如何处理匹配的字符串(在>和行末之间),因为read有点通用,我把它放在了一个表格中。

s=[[
>Hello World
text,1000
>A line that is way too long to fit in this small space won't be accepted in the table.
text2,200
>Some text on the last line]]

t={}
for match in s:gmatch('>([^\n]+)') do
    if #match<50 then
        t[#t+1]=match
        print(match)
    end
end

这也取决于哪个字节(串)表示该字符串中的行尾。 可能的选项是:\n(UNIX,Mac和其他理智的世界)或 \r\n(Windows)。

2012-01-18 08:07:59