Notepad++.a regular expression. how to find duplicate text by key features?

63 Views Asked by At

I'm trying to find and replace specific text in a file. And replace it only after a certain match first, because There is no need to change the same text in other places.

<component class="npc" macro="character_argon_male_afr_pilot_01_macro" connection="parentconnection" name="Элан Сламер" code="PRD-472" owner="player" known="1" read="0" page="10105" lastspeaktime="50747.875" id="[0x607c686]">
<listeners>
<listener listener="[0x4165ad0]" event="killed"/>
</listeners>
<movement class="crossconnection">
<additionaloffset/>
<attached component="[0x4165ad0]" connection="con_control01"/>
<time start="50801.319"/>
<offset>
<position x="-0.006" y="-24.275" z="32.788"/>
</offset>
</movement>
<offset>
<position x="0.0164" y="-1.282" z="0.3796"/>
</offset>
<render>
<parameter flags="override" owner="[0x607c686]" name="mat_dynamicglow" type="glow">
<persistent start="47413.071" interval="0"/>
</parameter>
</render>
<traits flags="remotecommable">
<skill type="morale" value="5"/>
<skill type="piloting" value="6"/>
</traits>

it is necessary to change the value in the skill lines, but only for a character of the NPC class belonging to the player (owner = player).

and another question is when what is selected. this is a string, how do I specify any character?

I tried to build a regular expression and all I achieved was selecting the desired NPC and the line after it. (.class="npc".owner="player".)(\n.$) but I just can’t get to the required values.

2

There are 2 best solutions below

0
Graf On BEST ANSWER

so, the answer to my problem looks like this.

(?'1'<component class="npc".+owner="player".+\n)(?'2'.+\n)*?(?'3'<traits flags.+\n)\K(?'4'<skill.+value="\d{1,2}".+\n)*

regex

replace result

thank to draqula for a hint on the idea

1
draqula On

Regex is not instrument you should use for this task. Any library that can parse xml on your language of choice would do.

But if you have to do it in regex for some reason you can use this expression for search: (?'Other'class="npc".+owner="player".+skill.*value=")(?'Target'.+)(?=") with ungreedy and single-line flags.

And this as replacement value ${Other}12

Basically you just have to put all your conditions in 'Other' group with gaps (.+) and target text in 'Target' group. Then you just place contents of 'Other' group back on substitution and replace value next to it.