0

I have a huge xml file, where i need to find and replace some parts. In total it occurs around 2100 times, so i hoped i could solve this by using the advanced tools in Notepad++. It doesnt have to be notepad++, so if there is another solution, im helped as well.

The find occurrence looks like this:

        <condition test="contains" flags="90">
          <category>
            <name internal="Type">Type</name>
          </category>
          <property>
            <name internal="parameter">Code</name>
          </property>
          <value>
            <data type="wstring">21.12</data>
          </value>
        </condition>

Everything is consistent, except for the number in the data part 21.12 This number is different in all the 2100 cases.

So i want to Find and replace, but ise that number as some kind of wildcard that is used in the find AND replace. The part where i have to find works as it should be with regex (Replace using RegEx in notepad++ ). But i am not able to paste the part of the regex.

So in short, if my code looks like this:

<code>6</code>
<code>7</code>
<code>8</code>

i want to find/replace to make it look like this:

<code>6</code>
<extraCode>6</extraCode>
<code>7</code>
<extraCode>7</extraCode>
<code>8</code>
<extraCode>8</extraCode>

Would this be possible, of would i need some other software, or make some C# script to do this?

Dante1986
  • 58,291
  • 13
  • 39
  • 54

3 Answers3

4

This answers directly to the second part of your question. You may try the following find and replace:

Find:

<code>(\d+)</code>

Replace:

<code>$1</code>\n<extraCode>$1</extraCode>

If this does not meet your requirements, then you should have phrased your question differently.

Note: If the contents of the <code> tags could be something besides numbers, then match on this pattern:

<code>([^<]+)</code>
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
2
  • Ctrl+H
  • Find what: <data type="wstring">(\d+(?:\.\d+)?)</data>
  • Replace with: $0\n<extraCode>$1</extraCode>
  • check Wrap around
  • check Regular expression
  • Replace all

Explanation:

(         : start group 1
  \d+     : 1 or more digits
  (?:     : start non capture group
    \.    : a dot
    \d+   : 1 or more digits
  )?      : end group, optional
)         : end group 1

Replacement:

$0          : the whole match
\n          : a linebreak, you could use \r\n

$1          : content of group 1 (ie. the value)

Result for given example:

    <condition test="contains" flags="90">
      <category>
        <name internal="Type">Type</name>
      </category>
      <property>
        <name internal="parameter">Code</name>
      </property>
      <value>
        <data type="wstring">21.12</data>
<extraCode>21.12</extraCode>
      </value>
    </condition>
Toto
  • 89,455
  • 62
  • 89
  • 125
1

You could use $0\n<extraCode>$1<extraCode> for replace. Therefore you would need to find like this: <code>(\d+)<code> you can expand this if needed by adding more subgroups to match.

EDIT: Seems like I was too slow ;)