小编use*_*563的帖子

Lua标点符号字符串模式中包含哪些字符(%p)?

我无法找到哪些字符复合了"%p"Lua中设置的标点符号的文档.

lua punctuation lua-patterns

8
推荐指数
3
解决办法
1480
查看次数

将Lua string.match输出存储到数组

通常,我使用两个变量来存储类似以下内容的输出:

a = {'alarm boy car dentist','alarm car dentist elephant','alarm dentist elephant fabulous','alarm elephant fabulous goat'}
k, v = string.match(a[1], 'alarm dentist (%w+) (%w+)' )
print(k, v)
elephant fabulous
Run Code Online (Sandbox Code Playgroud)

但我不想使用两个变量,而是将其存储在数组或表中。

我的最终目标是创建一个函数,在该函数中输入数组(在此例中为“ a”)和模式(在此例中为“ alarm dentist(%w +)(%w +)(%w +)”),并返回所需的附带词(如果找到),否则为“ nil”。问题是模式查找的单词数是不确定的。在这种情况下为2,但我希望用户能够输入任何模式,即“警报牙医(%w +)(%w +)(%w +)(%w +)”或“警报牙医(%w +)”。

所以到目前为止,这是我的想法:(我正在Ubuntu 12.04LTS中使用命令行工具对其进行测试)

a = {'alarm boy car dentist','alarm car dentist elephant','alarm dentist elephant fabulous','alarm elephant fabulous goat'}
function parse_strings (array, pattern)
words = nil
for i=1, #array do
    c = string.match(array[i], pattern)
    if c ~= nil then …
Run Code Online (Sandbox Code Playgroud)

arrays string lua lua-patterns

4
推荐指数
1
解决办法
2140
查看次数

标签 统计

lua ×2

lua-patterns ×2

arrays ×1

punctuation ×1

string ×1