Lua使用特定模式分割字符串 [英] Lua split string using specific pattern
问题描述
我需要使用特定模式-"分割输入文件的每一行.我离解决方案不远,但是我的代码实际上也拆分了单个空格.文件的每一行的格式如下:
i need to split each row of a input file using the specific pattern " - ". I'm not so far from solution but my code actually splits also single spaces. Each row of the file is formatted as follow:
NAME - ID - USERNAME - GROUP NAME - GROUP ID - TIMESTAMP
name字段可能包含空格,与组名和时间戳相同,例如这样的行
name field may have spaces, same as group name and timestamp, for example a row like that
LUCKY STRIKE - 11223344 - @lucky - CIGARETTES SMOKERS - 44332211 - 11:42 may/5th
有效. 因此,这些标记化的值应存储在表中. 这是我的代码:
is valid. So these tokenized values should be stored inside a table. Here is my code:
local function splitstring(inputstr)
sep = "(%s-%s)"
local t={} ; i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
print("=========="..t[1].."===========")
print("=========="..t[2].."===========")
print("=========="..t[3].."===========")
return t
end
当我运行它时,将幸运"放在第一字段中,将罢工放在第二字段中,将id放在第三字段中. 有没有一种方法可以在第一个字段中存储幸运罢工",仅按指定的模式进行解析? 希望你们能帮助我.
when i run it, puts "lucky" in first field, strike in second field, the id inside third field. Is there a way to store "lucky strike" inside first field, parsing ONLY by pattern specified? Hope you guys could help me.
p.s.我已经看过lua手册,但并没有太大帮助...
p.s. I already see the lua manual but didn't help me so much...
推荐答案
您所拥有的东西有些问题.
There are a few things wrong with what you have.
首先,-
是Lua模式中的重复符号:
http://www.lua.org/manual/5.2/manual.html #6.4.1
Firstly, -
is a repetition symbol in Lua patterns:
http://www.lua.org/manual/5.2/manual.html#6.4.1
您需要使用%-
来获取文字-
.
You need to use %-
to get a literal -
.
我们还没有完成:产生的gmatch调用是string.gmatch(inputstr, "[^%s%-%s]+")
.由于分隔符模式位于[]内,因此它是一个字符类.它说给我所有不是空格或-的东西,并尽可能地贪婪",这就是为什么它停在第一个空格字符处.
We're not done: The resulting gmatch call is string.gmatch(inputstr, "[^%s%-%s]+")
. Since your separator pattern is inside [], it's a character class. It says "Give me all the things that aren't a space or a -, and be as greedy as you can", which is why it stops at the first space character.
您最好的选择是做类似的事情:
Your best bet is to do something like:
local function splitstring(inputstr)
sep = "%-"
local t={} ; i=1
for str in string.gmatch(inputstr, "[^"..sep.."]+") do
t[i] = str
i = i + 1
end
print("=========="..t[1].."===========")
print("=========="..t[2].."===========")
print("=========="..t[3].."===========")
return t
end
哪个产量:
==========LUCKY STRIKE ===========
========== 11223344 ===========
========== @lucky ===========
...现在可以独立解决值周围空格的问题.
... And now independently fix the problem of the spaces around the values.
这篇关于Lua使用特定模式分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!