I have multiple text files which have various xpaths in their content. I want to use Notepad to add one new node in these xpaths, but there are some exceptions where I dont want to do it and due to them I'm struggling with preparing the right RegEx statement.
The goal is to add FpML node in xpath after allocation node with below exceptions:
- If allocation is preeceded by CRD_Structured
- If node after allocation is FT_Extension
Note that allocation is repetable node and therefore in these text files it might be denoted with specific index in [].
Examples:
- allocation[Out1]/@fpmlVersion --> allocation[Out1]/FpML/@fpmlVersion
- allocation[Int1]/trade --> allocation[Int1]/FpML/trade
- allocation[Out1]/FT_Extension --> no change
- pathString="allocation[]" --> no change
- CRD_Structured/allocation[FindAllocOut1]/TS_ORDER_ALLOC --> no change
CodePudding user response:
I would not try and find one regex/replace to achieve this. Instead I would make the change in several steps. In brief, I would (in steps 2 and 3 below) insert a marker string into allocation
for all cases that should not be changed then (at step 4) insert the wanted text and finally (step 5) remove the marker string.
In more detail.
- Choose a marker string that does not occur within the text. The string
!!!
is used below. - Regex replace
(CRD_Structured/a)(llocation)
with\1!!!\2
. - Regex replace
(a)(llocation\[\w \]/FT_Extension)
with\1!!!\2
. - Replace
(a)(llocation\[\w \]/)
with\1!!!\2FpML/
. - Replace
!!!
with nothing.
Note that step 4 also inserts the marker string. This is to prevent multiple insertions of FpML/
.
Item 4 in the question is not clear. It may be that an addition to steps 2 and 3 is needed. This addition would regex replace ^(a)(llocation\[\w*\])$
with \1!!!\2
. This assumes that the path string is the complete line.