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 


[XML][RegEx] Need help with advanced multi-line S&R

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
kranao8472
Newbie cheater
Reputation: 0

Joined: 19 Aug 2013
Posts: 20

PostPosted: Tue Dec 16, 2014 3:51 am    Post subject: [XML][RegEx] Need help with advanced multi-line S&R Reply with quote

I'm a total RegEx beginner and I have no problem admitting I don't know what I'm doing -- with RegEx that is. I do know what I'm doing in the XML code - at least insofar as knowing what I'm trying to change. Perhaps someone here could point me in the right direction?

I'm using the built-in RegEx search/replace module in NP++ and I want to take the following instance:

Code:
<component class="snaptime" macro="macRXRA42C" connection="con_sub_rrL3" owner="NRM14" id="[0x457d]">
<listeners>
<listener listener="[0x4576]" event="ev_sub_rec"/>
</listeners>
<callers>
<caller type="CLXA" value="6"/>
<caller type="AKTB" value="1"/>
<caller type="GX1A" value="3"/>
<caller type="YB19" value="7"/>
<caller type="AFAF" value="5"/>
</callers>
<entity type="SCRR9390" control="1"/>
<connections/>
</component>


... and change every caller value to 1. Note this is the ONLY thing I want to change. Everything else needs to be left untouched.

First problem: No, it is not as simple as running:

Code:
<caller type=(".*") value=(".")


...and replacing with...

Code:
<caller type=\1 value="1"


... because the "owner" in each component changes frequently in the same file and this value change needs to only affect the same "NRM14" owner.

Problem 2: Everything aside from owner inside each component changes at least once even when owner remains the same, so I can't use the entire component instance as an anchor.

Problem 3: The amount of caller types per callers list varies. Likewise, there are often multiple listener events listed within "listeners". I don't know how to compensate for that using RegEx.

Problem 4: The entity may not be specified within all component instances. Sometimes another file specifies it instead, and mismatches are bad. So I need to make sure the search/replace happens within each component with the same owner and ONLY changes any/all caller values within to 1, otherwise I risk Bad Things™.

I could just spam the "replace" button in NP++ on the above "simple" RegEx S&R example whilst keeping an eye out to make sure it's the same owner, but that'd take days. We're talking hundreds of thousands of hits, meaning I'd be sitting here tapping the "replace" button and hitting CTRL+Z every time I slip up. Tried that, not worth it.

I just wish there was a more intuitive method of doing this. Something like:

Code:
AFTER.EACH <component .* (owner="NRM14") .*>
WHERE <caller type=".*" value=".*"/>
CHANGE.TO <caller type=".*" value="1"/>


That'd solve the entire thing right there.
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Tue Dec 16, 2014 4:33 am    Post subject: Reply with quote

Search for:
<caller type="([a-zA-Z0-9]+)" value="[0-9]+"/>

Replace with:
<caller type="\1" value="1"/>

As for the idea of what you want to do, you can do that exact thing with Linq using C#.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
panraven
Grandmaster Cheater
Reputation: 62

Joined: 01 Oct 2008
Posts: 958

PostPosted: Tue Dec 16, 2014 7:01 am    Post subject: Reply with quote

I think it need a regEx capable of non-greedy match.
In ce lua it is something like .- instead of greedy version .*

Here a lua script should do the requirement, hope it can be adapted to your script language.

It use string.gsub with a replacement function instead of string-to-string replacement. The owner id is test inside function before do secondary replacement.
The xml is simplified. It is also becoz there seems bug in non-greedy match in lua5.1, that it seems if the pattern is too long the matching become greedy... Please ignore if not using lua5.1 script.

Code:
local xml = [=[
<com own='abc15' a>
<caller type="CLXA" value="6"/>
<caller type="AKTB" value="1"/>
<caller type="GX1A" value="3"/>
<caller type="YB19" value="7"/>
<caller type="AFAF" value="5"/>
</com>
<com own='def42' b>
<caller type="CLXA" value="6"/>
<caller type="AKTB" value="1"/>
<caller type="GX1A" value="3"/>
<caller type="YB19" value="7"/>
<caller type="AFAF" value="5"/>
</com>
<com own='abc15' c>
<caller type="CLXA" value="6"/>
<caller type="AKTB" value="1"/>
<caller type="GX1A" value="3"/>
<caller type="YB19" value="7"/>
<caller type="AFAF" value="5"/>
</com>
]=]

local s = string.gsub(xml,"(<com.-=%'([a-zA-Z0-9]+).-</com>)",function(a,b) -- a is whole matched pattern, b is owner id
  if b == 'abc15' then
    a = string.gsub(a,'(<caller type="[a-zA-Z0-9]+" value=)"(%d+)','%1"1')
  end
  return a
end)
print(s)
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming 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