2017-11-07 7 views
1
function splitSat(str, pat, max, regex) 
    pat = pat or "\n" --Patron de búsqueda 
    max = max or #str 

    local t = {} 
    local c = 1 

    if #str == 0 then 
      return {""} 
    end 

    if #pat == 0 then 
      return nil 
    end 

    if max == 0 then 
      return str 
    end 

    repeat 
      local s, e = str:find(pat, c, not regex)  -- Dentro del string str, busca el patron pat desde la posicion c 
                 -- guarda en s el numero de inicio y en e el numero de fin 
      max = max - 1 
      if s and max < 0 then 
        if #(str:sub(c)) > 0 then   -- Si la longitud de la porcion de string desde c hasta el final es mayor que 0 
          t[#t+1] = str:sub(c) 
        else                                              values 
          t[#t+1] = "" --create a table with empty 
        end 
      else 
        if #(str:sub(c, s and s - 1)) > 0 then   -- Si la longitud de la porcion de string str entre c y s 
          t[#t+1] = str:sub(c, s and s - 1) 
        else 
          t[#t+1] = ""   --create a table with empty values 
        end 
      end 
      c = e and e + 1 or #str + 1 
    until not s or max < 0 

    return t 
    end 

この機能が何をしているのか知りたいです。私はそれが文字列とパターンを取る一種のテーブルを作ることを知っています。特に*t[#t+1] = str:sub(c, s and s - 1)*が何をしているのですか?この関数はLuaで何をしていますか?

+0

("abcde"):sub(2,4) == aでtartingとbabている数値指標)で終わります。だから私はhttps://stackoverflow.com/questions/17974622/what-does-mean-in-luaを見上げた。コメントはスペイン語で書かれているので、私はGoogle翻訳を使ってその意味を理解しました。 –

答えて

0

私が得たことから、長い文字列を特定のパターンに一致する部分文字列に分割し、パターンマッコの間のすべてを無視します。たとえば、文字列11aa22とパターン\d\dが一致すると、表["11", "22"]になります。

t[#t+1] = <something>テーブルtの終わりに値を挿入し、それがtable.insert(t, <something>)

#t同様のは、例えば、(すなわち、連続的な数値インデックスを有するテーブルである)#[1, 2, 3] == 3の配列の長さを返します

str:sub(c, s and s - 1)は、多くのルアー機能を利用しています。 s and s - 1は、がnilでない場合はs-1と評価され、それ以外の場合は0と評価されます。 >エラー

str:sub(a, b)を投げる - snil

  • 10 and 10 - 1 == 9
  • 10 - 1 == 9
  • nil and nil - 1 == nil
  • nil - 1がされた場合だけでs-1はエラーをスローします部分文字列を返すだけです私はLuaので働いたことはない"bcd"

+0

ありがとうございますDark !! – gonzalodt

関連する問題