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 


String patterns %b() exclude \(\)

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
sir-gunny
Advanced Cheater
Reputation: 0

Joined: 15 Mar 2012
Posts: 80

PostPosted: Wed Feb 21, 2024 3:58 am    Post subject: String patterns %b() exclude \(\) Reply with quote

Hi.

Does anyone have any idea how I can exclude \(text\) from string:gsub("%b()","")?

Code:
s = [[\(keep (this not) this\) Hello (remove ( me)please) world]]
print(s:gsub("%b()",""))


Output:
Code:
\ Hello  world


But i need this:
Code:
\(keep this\) Hello world


Thx
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 32

Joined: 16 Feb 2017
Posts: 1260

PostPosted: Wed Feb 21, 2024 9:23 am    Post subject: Reply with quote

Code:
s = [[\(keep (this not) this\) Hello (remove ( me)please) world \(keep (this not) this\)]]
res, repcount = string.gsub(s, "( %b())", "")
print(res) --> \(keep this\) Hello world
print(repcount)


--res--> \(keep this\) Hello world \(keep this\)
--repcount--> 3

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4307

PostPosted: Wed Feb 21, 2024 2:04 pm    Post subject: This post has 1 review(s) Reply with quote

More generally, use a frontier pattern to exclude parenthesis preceded by a backslash:
Code:
local s = [[\(keep (this not) this\) Hello (remove ( me)please) world]]
print((s:gsub('%f[\\(]%b()','')))

-- \(keep  this\) Hello  world
Note that there are 2 spaces where the substitution occurred. Deal with that as you see fit.
If you always want there to be at least one space, perhaps this is fine:
Code:
local s = [[\(keep (this not) this\) Hello (remove ( me)please) world]]
print((s:gsub(' ?%f[\\(]%b() ?',' ')))

-- \(keep this\) Hello world

If you need more advanced logic, use a function to figure it out.
Code:
local s = [[\(keep (this not) this\) Hello (remove ( me)please) world]]
print((s:gsub('(.?)%f[\\(]%b()(.?)',function(pre, post)
  if pre == ' ' and post == ' ' then
    return ' '
  else
    return ''
  end
end)))

-- \(keep this\) Hello world

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 32

Joined: 16 Feb 2017
Posts: 1260

PostPosted: Wed Feb 21, 2024 3:45 pm    Post subject: Reply with quote

I thought I provided a solution that produced the desired result.

But the lesson to be learned from this is:

Exclude:
Code:
%f[] , %f()

Delete:
Code:
%b(), %b[], %b<> -- etc.


Oki ..

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
sir-gunny
Advanced Cheater
Reputation: 0

Joined: 15 Mar 2012
Posts: 80

PostPosted: Thu Feb 22, 2024 4:14 am    Post subject: Reply with quote

Hi.

Thank you very much for your help. Unfortunately my question was not clear enough, sorry. The "()" part can be anywhere in the string e.g. at the beginning, between words, after special characters etc. . Therefore ParkourPenguin's solution is ideal for me. Until now I did not know frontier pattern. Thanks for that! Smile
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 55

Joined: 01 Oct 2008
Posts: 943

PostPosted: Thu Feb 22, 2024 3:53 pm    Post subject: Reply with quote

If you can find an unique literal string (z) NOT in the string to be search, eg chr(0), then you can gsub '\(' with z, do your need gsub, and finally gsub z with the original. You may find the unique z by other way if cannot be sure a fixed string.

UPDATED:
Actually, above is not working.
1. the str like '\(' need to be escape for pattern matching, ie ->'\%(';
2. chr(0) seems special, cause garbage result, not know why, but using chr(1), chr(2) etc ok;
Code:

local z1, z2, r1, r2 = string.char(1), string.char(2), [[\%(]], [[\%)]]
print(
s:gsub(r1,z1)
 :gsub(r2,z2)
 :gsub('%b()','')
 :gsub(z2,r2)
 :gsub(z1,r1)
)


Also, it is not a general solution, only work for removing %b();
if it need to process content in %b(), then the result may not correct as %b() may contain replaced char.

_________________
- Retarded.
Back to top
View user's profile Send private message
sir-gunny
Advanced Cheater
Reputation: 0

Joined: 15 Mar 2012
Posts: 80

PostPosted: Fri Feb 23, 2024 3:40 am    Post subject: Reply with quote

My first solution was similar. I just replaced the characters with "-#" and "#-". But I suspected that there was a more "elegant" solution.

In my case, the content between "()" only consists of information from the programmers. This is not displayed in the game.
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