Home > other >  RegEx in Notepad to replace text in files with some exclusions
RegEx in Notepad to replace text in files with some exclusions

Time:01-19

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:

  1. If allocation is preeceded by CRD_Structured
  2. 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:

  1. allocation[Out1]/@fpmlVersion --> allocation[Out1]/FpML/@fpmlVersion
  2. allocation[Int1]/trade --> allocation[Int1]/FpML/trade
  3. allocation[Out1]/FT_Extension --> no change
  4. pathString="allocation[]" --> no change
  5. 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.

  1. Choose a marker string that does not occur within the text. The string !!! is used below.
  2. Regex replace (CRD_Structured/a)(llocation) with \1!!!\2.
  3. Regex replace (a)(llocation\[\w \]/FT_Extension) with \1!!!\2.
  4. Replace (a)(llocation\[\w \]/) with \1!!!\2FpML/.
  5. 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.

  • Related