View previous topic :: View next topic |
Author |
Message |
sir-gunny Advanced Cheater Reputation: 0
Joined: 15 Mar 2012 Posts: 81
|
Posted: Wed Feb 21, 2024 3:58 am Post subject: String patterns %b() exclude \(\) |
|
|
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:
But i need this:
Code: | \(keep this\) Hello world |
Thx |
|
Back to top |
|
|
AylinCE Grandmaster Cheater Supreme Reputation: 33
Joined: 16 Feb 2017 Posts: 1346
|
Posted: Wed Feb 21, 2024 9:23 am Post subject: |
|
|
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 _________________
|
|
Back to top |
|
|
ParkourPenguin I post too much Reputation: 143
Joined: 06 Jul 2014 Posts: 4382
|
|
Back to top |
|
|
AylinCE Grandmaster Cheater Supreme Reputation: 33
Joined: 16 Feb 2017 Posts: 1346
|
Posted: Wed Feb 21, 2024 3:45 pm Post subject: |
|
|
I thought I provided a solution that produced the desired result.
But the lesson to be learned from this is:
Exclude:
Delete: Code: | %b(), %b[], %b<> -- etc. |
Oki .. _________________
|
|
Back to top |
|
|
sir-gunny Advanced Cheater Reputation: 0
Joined: 15 Mar 2012 Posts: 81
|
Posted: Thu Feb 22, 2024 4:14 am Post subject: |
|
|
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! |
|
Back to top |
|
|
panraven Grandmaster Cheater Reputation: 55
Joined: 01 Oct 2008 Posts: 943
|
Posted: Thu Feb 22, 2024 3:53 pm Post subject: |
|
|
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 |
|
|
sir-gunny Advanced Cheater Reputation: 0
Joined: 15 Mar 2012 Posts: 81
|
Posted: Fri Feb 23, 2024 3:40 am Post subject: |
|
|
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 |
|
|
|