Lua字符串拆分

Lua字符串拆分,lua,coronasdk,Lua,Coronasdk,嗨,我在JavaScript中有这个函数: function blur(data) { var trimdata = trim(data); var dataSplit = trimdata.split(" "); var lastWord = dataSplit.pop(); var toBlur = dataSplit.join(" "); } 这需要一个字符串,比如“你好,我的名字是鲍勃”,然后返回 toBlur=“你好,我的名字是”和lastW

嗨,我在JavaScript中有这个函数:

    function blur(data) {
    var trimdata = trim(data);
    var dataSplit = trimdata.split(" ");
    var lastWord = dataSplit.pop();
    var toBlur = dataSplit.join(" ");
 }
这需要一个字符串,比如“你好,我的名字是鲍勃”,然后返回 toBlur=“你好,我的名字是”和lastWord=“鲍勃”


有没有办法让我用Lua重新写

您可以使用Lua的模式匹配功能:

function blur(data) do
    return string.match(data, "^(.*)[ ][^ ]*$")
end
模式是如何工作的

^      # start matching at the beginning of the string
(      # open a capturing group ... what is matched inside will be returned
  .*   # as many arbitrary characters as possible
)      # end of capturing group
[ ]    # a single literal space (you could omit the square brackets, but I think
       # they increase readability
[^ ]   # match anything BUT literal spaces... as many as possible
$      # marks the end of the input string
因此,
[][^]*$
必须匹配最后一个单词和前面的空格。因此,
(.*)
将返回它前面的所有内容

要更直接地翻译JavaScript,首先请注意Lua中没有
split
函数。不过还有一个
table.concat
,它的工作原理类似于
join
。由于必须手动进行拆分,因此可能会再次使用模式:

function blur(data) do
    local words = {}
    for m in string.gmatch("[^ ]+") do
        words[#words+1] = m
    end
    words[#words] = nil     -- pops the last word
    return table.concat(words, " ")
end

gmatch
不会立即为您提供一个表,而是为所有匹配项提供一个迭代器。因此,您可以将它们添加到自己的临时表中,并在该表上调用
concat
words[#words+1]=…
是一种Lua习惯用法,用于将元素附加到数组的末尾。

您可以使用Lua的模式匹配功能:

function blur(data) do
    return string.match(data, "^(.*)[ ][^ ]*$")
end
模式是如何工作的

^      # start matching at the beginning of the string
(      # open a capturing group ... what is matched inside will be returned
  .*   # as many arbitrary characters as possible
)      # end of capturing group
[ ]    # a single literal space (you could omit the square brackets, but I think
       # they increase readability
[^ ]   # match anything BUT literal spaces... as many as possible
$      # marks the end of the input string
因此,
[][^]*$
必须匹配最后一个单词和前面的空格。因此,
(.*)
将返回它前面的所有内容

要更直接地翻译JavaScript,首先请注意Lua中没有
split
函数。不过还有一个
table.concat
,它的工作原理类似于
join
。由于必须手动进行拆分,因此可能会再次使用模式:

function blur(data) do
    local words = {}
    for m in string.gmatch("[^ ]+") do
        words[#words+1] = m
    end
    words[#words] = nil     -- pops the last word
    return table.concat(words, " ")
end

gmatch
不会立即为您提供一个表,而是为所有匹配项提供一个迭代器。因此,您可以将它们添加到自己的临时表中,并在该表上调用
concat
words[#words+1]=…
是一种Lua习惯用法,用于将元素附加到数组的末尾。

感谢您的响应,使用第一位代码
return string.match(data,“^(.*)[^]*$”
我能够返回字符串的第一位,如何进行单独的sting匹配以仅返回最后一个单词?实际上,您可以在一个调用中同时执行这两个操作:
返回字符串。match(数据,“^(.*)[([^]*)$”
将有两个返回值(每个捕获组一个)。如果在单独的调用中需要它,只需一次使用一个捕获组。我正在考虑向两个不同的变量返回两个单独的值
local hiddentxt=string.match(dataTable.lines[I].text,“^(.*)[^]*$”
,因此在本例中它将返回“Hello my name is”我还需要另一个
local shownText=..
返回“bob”,因为我需要对不同变量中的值进行稍微不同的处理。我想我知道你的意思。执行类似的操作
local hiddentxt,shownText=string.match(dataTable.lines[i].text,“^(.*)[([^]*)$”
返回这两个变量values@Tam2要挑剔,,我认为这不会首先修剪绳子。若要合并,可以使用类似于
^[]*(.*)[]([^]*)[]*$
的内容。这应该确保空间在捕获组之外匹配,这样它们就不会成为结果的一部分。(我可能错了,因为我还没有实际使用过这么多,上周才开始使用Lua。)感谢您的响应,使用了第一位代码
return string.match(data),“^(.*[^]*$”
我能够返回字符串的第一位,如何进行单独的sting匹配以仅返回最后一个单词?实际上,您可以在一个调用中同时执行这两个操作:
返回字符串。match(数据,“^(.*)[([^]*)$”
将有两个返回值(每个捕获组一个)。如果在单独的调用中需要它,只需一次使用一个捕获组。我正在考虑向两个不同的变量返回两个单独的值
local hiddentxt=string.match(dataTable.lines[I].text,“^(.*)[^]*$”
,因此在本例中它将返回“Hello my name is”我还需要另一个
local shownText=..
返回“bob”,因为我需要对不同变量中的值进行稍微不同的处理。我想我知道你的意思。执行类似的操作
local hiddentxt,shownText=string.match(dataTable.lines[i].text,“^(.*)[([^]*)$”
返回这两个变量values@Tam2要挑剔,,我认为这不会首先修剪绳子。若要合并,可以使用类似于
^[]*(.*)[]([^]*)[]*$
的内容。这应该确保空间在捕获组之外匹配,这样它们就不会成为结果的一部分。(但我可能错了,因为我还没有实际使用过这么多,上周才开始使用Lua。)