title: how to find part of a string in lua, and split into 3 parts ? (before, matched, after)
body: <p>say I have this string, and I want to split "blue"</p>
  
  <pre><code>local str = "The mouse walked by the blue cheese"
  </code></pre>
  
  <p>to make it like this</p>
  
  <pre><code>local pre = "The mouse walked by the "    
  local matched = "blue"    
  local post = " cheese"
  </code></pre>
  
  <p>any suggestions for this? I searched for sometime and feel stuck.</p>
  
  <p>I tried this, it works for english but seems not to work in arabic :(</p>
  
  <pre><code>function split(string, delimiter)
    local result = { }
    local from = 1
    local delim_from, delim_to = string.find( string, delimiter, from )
    while delim_from do
      table.insert( result, string.sub( string, from , delim_from-1 ) )
      from = delim_to + 1
      delim_from, delim_to = string.find( string, delimiter, from )
    end
    table.insert( result, string.sub( string, from ) )
    return result
  end
  
  local myStr = "The mouse walked by the blue cheese"
  
  sep = split(myStr, "blue")
  print("part1 1: "..sep[1])
  print("part2 2: "..sep[2])
  </code></pre>
  
  <p><strong>EDITED ===================================================</strong></p>
  
  <p>please tell me what to do in case there are multiple target words to split
  for example</p>
  
  <pre><code>local myStr = "the mouse ate the cheese, the mouse ate the cheese!, lot of cheese there !"
  </code></pre>
  
  <p>the problem here that it splits it into multiple ones, what is needed is </p>
  
  <blockquote>
    <p>before = "the mouse ate the " after = ", the mouse ate the cheese!,
    lot of cheese there !"</p>
  </blockquote>
  
  <p>I made this, it works for english, but not for arabic</p>
  
  <pre><code>function split4(pString, pPattern)
  
      local didFind = 0
  
      if string.find(pString,".") then
          pString = string.gsub(pString,"%.","'.'")
      end
  
      if pPattern == "." then
          pPattern = "'.'"
      end
  
      local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
      local FinalTable = {}
      local fpat = "(.-)" .. pPattern
      local last_end = 1
      local s, e, cap = pString:find(fpat, 1)
      while s do
          if s ~= 1 or cap ~= "" then
              print("Found")
              table.insert(Table,cap)
              didFind = didFind + 1
          end
          last_end = e+1
          s, e, cap = pString:find(fpat, last_end)
      end
      if last_end &lt;= #pString then
          print("Found NOT")
          cap = pString:sub(last_end)
          table.insert(Table, cap)
      end
      print(#Table)
      print("totalDidFind "..didFind)
  
  
      FinalTable[1] = Table[1]
  
      if (didFind &gt; 2) then
        local endString = ""
        for z = 2 , didFind do 
          endString = endString..Table[z]..pPattern
        end
        FinalTable[2] = endString
  
      end
  
      totalDidFind = 0
      return FinalTable
  end
  </code></pre>