Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


lua string>>Intercepting and replacing strings

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
.lua
Expert Cheater
Reputation: 1

Joined: 13 Sep 2018
Posts: 203

PostPosted: Fri May 31, 2019 2:37 pm    Post subject: lua string>>Intercepting and replacing strings Reply with quote

Code:

function getStr(s,t,e,h)
  if string.find(s,t) and string.find(s,e) then
    if h==nil then
      if string.len(e)>=2 then
        local t1=string.find(s,t)
        local e1=string.find(s,e)-1+string.len(e)
        local z1=string.sub(s,t1,e1)
        return z1
      else
       local t2=string.find(s,t)
       local e2=string.find(s,e)
       local z2=string.sub(s,t2,e2)
       return z2
      end
    else
      if string.len(t)>=2  then
        local t3=string.find(s,t)+string.len(t)
        local e3=string.find(s,e)-1
        local z3=string.sub(s,t3,e3)
        return z3
      else
        if string.len(e)>=2 then
          local t4=string.find(s,t)+1
          local e4=string.find(s,e)-1
          local z4=string.sub(s,t4,e4)
          return z4
        else
          local t5=string.find(s,t)+1
          local e5=string.find(s,e)-1
          local z5=string.sub(s,t5,e5)
          return z5
        end
      end
    end
  end
end
local s = "Hello, can you help me simplify the code?"
print(getStr(s,'H','code?'))
print(getStr(s,'H','code?',0))

print(getStr(s,', c','the'))
print(getStr(s,'o, ','the',0))

Hello, can you help me simplify the code?

Or you can give me the simplest code you think. Thank you.
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 62

Joined: 01 Oct 2008
Posts: 959

PostPosted: Fri May 31, 2019 5:58 pm    Post subject: Reply with quote

Code:
function getStrBoundByHeadTail(s,h,t, excludeHT)
  local h1,h2 = s:find(h) -- return beginning and ending index of h if found
  local t1,t2 = s:find(t) -- ditto for t
  if h1 and t1 then -- find both
    local hh = excludeHT and h2+1 or h1 -- ending+1 or beginning index of h
    local tt = excludeHT and t1-1 or t2 -- beginning-1 or ending index of t
    return s:sub(hh,tt)
  end
  return nil
end


The s:find(x) treat x as regular expression instead of plain text, this may cause problem if x contain some regular expression characters. It can be force to plain text by following function.
Code:

function toPlainText(s)return (s:gsub("%W","%%%1"))end

_________________
- Retarded.


Last edited by panraven on Sat Jun 01, 2019 2:22 am; edited 2 times in total
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Fri May 31, 2019 10:20 pm    Post subject: Reply with quote

My turn:

Code:
local function splitString(str, start, last)
 local dummy = string.gsub(str, start, "")
 local text = string.gsub(dummy, last, "")
 return (text:gsub("^%s*", ""))
end

local str = "Hello, can you help me simplify the code?"

-- Test
print(splitString(str, 'Hello, ', ''))
-- , can you help me simplify the code?
print(splitString(str, 'can', 'you help '))
-- Hello,  me simplify the code?
dumb = splitString(str, 'can', 'you help ')
print(splitString(dumb, 'Hello,', 'code%?'))
--me simplify the
print(splitString(str, 'Hel', ''))
-- lo, can you help me simplify the code?

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
.lua
Expert Cheater
Reputation: 1

Joined: 13 Sep 2018
Posts: 203

PostPosted: Sat Jun 01, 2019 12:12 am    Post subject: Reply with quote

panraven wrote:
Code:
function getStrBoundByHeadTail(s,h,t, excludeHT)
  local h1,h2 = s:find(h) -- return beginning and ending index of h if found
  local t1,t2 = s:find(t) -- ditto for t
  if h1 and t1 then -- find both
    local hh = excludeHT and h2+1 or h1 -- ending+1 or beginning index of h
    local tt = excludeHT and t1-1 or t2 -- beginning-1 or ending index of t
    return s:sub(hh,tt)
  end
end
Code:
function getStrBoundByHeadTail(s,h,t, excludeHT)
  local h1,h2 = s:find(h) -- return beginning and ending index of h if found
  local t1,t2 = s:find(t) -- ditto for t
  if h1 and t1 then -- find both
    local hh = excludeHT and h2+1 or h1 -- ending+1 or beginning index of h
    local tt = excludeHT and t1-1 or t2 -- beginning-1 or ending index of t
    return s:sub(hh,tt)
  end
  if not t1 then return s:gsub(h,t) end
end

local s='1@2@3@4@5@6@7@8'

print(getStrBoundByHeadTail(s,'1@','@8',''))
--return:2@3@4@5@6@7(I don't understand)

print(getStrBoundByHeadTail(s,'1','8'))
--return:1@2@3@4@5@6@7@8(①)

print(getStrBoundByHeadTail(s,'@','-'))
--return:1-2-3-4-5-6-7-8(②)

print(getStrBoundByHeadTail(s,'@',''))
--return:blank(Why is it not 12345678? I don't understand)(①)

Please look at the code problem for me again.[/quote]
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 62

Joined: 01 Oct 2008
Posts: 959

PostPosted: Sat Jun 01, 2019 1:47 am    Post subject: Reply with quote

Quote:
print(getStrBoundByHeadTail(s,'1@','@8',''))
--return:2@3@4@5@6@7(I don't understand)

Any thing in lua is 'true' when interpreted as boolean value except 'false' , 'nil', "NaN" and absent parameter. So last "" in getStrBoundByHeadTail(s,'1@','@8','') means true.
Quote:
print(getStrBoundByHeadTail(s,'1','8'))
--return:1@2@3@4@5@6@7@8(①)

Any problem? exclueHT is false (as absent parameter), so '1' and '8' is not removed.
Quote:
print(getStrBoundByHeadTail(s,'@','-'))

this should return no parameter, since '-' not in s. Somehow in ce print() print nothing while print(nil) return an empty line. I edit the function to return nil instead if no h or t found, since some function may cause error if no parameter, eg. tostring().
Quote:
print(getStrBoundByHeadTail(s,'@',''))

s:find("") always return t1=1,t2=0, if h <found> then result s:sub(hh,tt)
<excludeHT=true>hh = h2+1(>1) tt = t1(is 1)-1=0, hh>tt so return ""
<excludeHT=false>hh = h1(>=1) tt = t2=0. hh>tt so return ""

_________________
- Retarded.
Back to top
View user's profile Send private message
.lua
Expert Cheater
Reputation: 1

Joined: 13 Sep 2018
Posts: 203

PostPosted: Sat Jun 01, 2019 2:44 am    Post subject: Reply with quote

Quote:

s:find("") always return t1=1,t2=0, if h <found> then result s:sub(hh,tt)
<excludeHT=true>hh = h2+1(>1) tt = t1(is 1)-1=0, hh>tt so return ""
<excludeHT=false>hh = h1(>=1) tt = t2=0. hh>tt so return ""

Code:
if h1 and t1 then
Change him into
Code:
if h1 and t1 and h~="" and t~="" then

Is that all right?
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 62

Joined: 01 Oct 2008
Posts: 959

PostPosted: Sat Jun 01, 2019 6:29 pm    Post subject: Reply with quote

sure if it fit your function's usage.
_________________
- Retarded.
Back to top
View user's profile Send private message
.lua
Expert Cheater
Reputation: 1

Joined: 13 Sep 2018
Posts: 203

PostPosted: Sat Jun 01, 2019 8:20 pm    Post subject: Reply with quote

panraven wrote:
sure if it fit your function's usage.
Yes, I've tested it and can solve my current string problem for the time being.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites