【问题标题】:Lua need to split at commaLua 需要以逗号分隔
【发布时间】:2013-10-16 06:21:29
【问题描述】:

我已经用谷歌搜索了,但我没有得到它。貌似这么简单的功能,Lua当然没有。

在 Python 中我会这样做

string = "cat,dog"
one, two = string.split(",")

然后我会有两个变量,一个 = cat。二 = 狗

如何在 Lua 中做到这一点!?

【问题讨论】:

标签: lua split match


【解决方案1】:

试试这个

str = 'cat,dog'
for word in string.gmatch(str, '([^,]+)') do
    print(word)
end

'[^,]' 表示“除逗号之外的所有字符,+ 符号表示“一个或多个字符”。括号创建捕获(在这种情况下并不真正需要)。

【讨论】:

    【解决方案2】:

    在页面顶部添加此拆分功能:

    function string:split( inSplitPattern, outResults )
      if not outResults then
        outResults = { }
      end
      local theStart = 1
      local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
      while theSplitStart do
        table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
        theStart = theSplitEnd + 1
        theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
      end
      table.insert( outResults, string.sub( self, theStart ) )
      return outResults
    end
    

    然后执行以下操作:

    local myString = "Flintstone, Fred, 101 Rockledge, Bedrock, 98775, 555-555-1212"
    
    local myTable = myString:split(", ")
    for i = 1, #myTable do
       print( myTable[i] ) -- This will give your needed output
    end
    

    欲了解更多信息,请访问:Tutorial: Lua String Magic

    继续编码......:)

    【讨论】:

      【解决方案3】:

      如果您可以使用库,答案是(在 Lua 中经常)use Penlight

      如果 Penlight 对你来说太重了,而你只想用 single 逗号分割一个字符串,就像你的例子一样,你可以这样做:

      string = "cat,dog"
      one, two = string:match("([^,]+),([^,]+)")
      

      【讨论】:

        【解决方案4】:

        string.split() 这样的函数在 Lua 中基本上是不必要的,因为你可以 在LPEG 中表达字符串操作。 如果您仍然需要专用功能,一种方便的方法是 定义一个分离器工厂(mk_splitter() 在下面的 sn-p 中) 然后您可以从中派生自定义拆分器。

        local lpeg      = require "lpeg"
        local lpegmatch = lpeg.match
        local P, C      = lpeg.P, lpeg.C
        
        local mk_splitter = function (pat)
          if not pat then
            return
          end
          pat            = P (pat)
          local nopat    = 1 - pat
          local splitter = (pat + C (nopat^1))^0
          return function (str)
            return lpegmatch (splitter, str)
          end
        end
        

        使用 LPEG 的优点是函数接受 有效的 Lua 字符串和模式都作为参数。

        以下是您将如何使用它来创建一个函数 在, 字符处拆分字符串:

        commasplitter = mk_splitter ","
        
        print (commasplitter [[foo, bar, baz, xyzzy,]])
        print (commasplitter [[a,b,c,d,e,f,g,h]])
        

        【讨论】:

          【解决方案5】:

          这就是我在 mediawiki 上的做法:

          str = "cat,dog"
          local result = mw.text.split(str,"%s*,%s*")
          -- result[0] will give "cat", result[1] will give "dog"
          

          其实,如果你不关心空格,你可以使用:

          str = "cat,dog"
          local result = mw.text.split(str,",")
          -- result[0] will give "cat", result[1] will give "dog"
          

          这里使用的 API 在 Scribunto MediaWiki 扩展中实现。这是split() method reference documentation,这是the source code for that。它依赖于 Scribunto 的 Lua 公用库中的许多其他功能,因此只有在您实际使用 MediaWiki 或计划导入大部分 Scribunto 公用库时,它才对您有效。

          【讨论】:

            【解决方案6】:

            -- 像 C strtok 一样,在一个分隔符上拆分(查找每个不包含任何分隔符的字符串)

            function split(source, delimiters)
                    local elements = {}
                    local pattern = '([^'..delimiters..']+)'
                    string.gsub(source, pattern, function(value) elements[#elements + 1] =     value;  end);
                    return elements
              end
            

            -- 例子: var elements = split("bye# bye, miss$ american@ pie", ",#$@ ") -- 返回 "bye" "bye" "miss" "american" "pie"

            【讨论】:

              【解决方案7】:

              还可以处理可选的空白:

              str = "cat,dog,mouse, horse"
              for word in str:gmatch('[^,%s]+') do
                  print(word)
              end
              

              输出将是:

              cat
              dog
              mouse
              horse
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多