NeOzay

patternFind

Aug 3rd, 2021 (edited)
89
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local mdl={}
  2.  
  3. function mdl.FindPattern(text, pattern, start)
  4.   start = start or 1
  5.  local a,b = string.find(text,pattern,start)
  6.   if a and b then
  7.     return string.sub(text,a,b),a,b
  8.   else
  9.     return nil
  10.   end
  11. end
  12.  
  13. function mdl.FindAllPattern(text,pattern,pcase)
  14. local case = pcase or 0
  15. local found = {}
  16. local start = 1
  17. local endd = 0
  18. while endd do
  19.   start,endd = string.find(text,pattern,start)
  20.   if start and endd then
  21.     table.insert(found,string.sub(text,start+case,endd))
  22.   start = endd
  23.   end
  24. end
  25. return found
  26. end
  27.  
  28. function mdl.capturePattern(text,pattern,pstart)
  29.  local start,stop,found = string.find(text,pattern,pstart)
  30.  return found,start,stop
  31. end
  32.  
  33. function mdl.captureAllPattern(text,pattern)
  34.   local start=1
  35.   local found={}
  36.   while start do
  37.     local value
  38.     _,start,value = string.find(text,pattern,start)
  39.     if value then
  40.       table.insert(found,value)
  41.     end
  42.   end
  43.   return found
  44. end
  45.  
  46. function mdl.segment(text,pattern)
  47.   local bs = 1
  48.   local be = 1
  49.   local bt = 1
  50.   local found = {}
  51.   while be do
  52.     be,bt = string.find(text,pattern,bs)
  53.     if be then
  54.       if be ~= bs then
  55.         table.insert(found,string.sub(text,bs,be-1))
  56.       end
  57.       bs = bt+1
  58.     elseif  bs <= string.len(text) then
  59.       table.insert(found,string.sub(text,bs,string.len(text)))
  60.     end
  61.   end
  62.    
  63.   return found
  64. end
  65.  
  66. return mdl
RAW Paste Data