1

I have an .xml file which contains many times the following web beacon

<span lang="EN" class="multilang">ANY TEXT TOTO</span>

I would like to make a "Find&Replace": each web beacon of this type (the text inside may differ) should be replace by

<span lang="FR" class="multilang">ANY TEXT TOTO</span>
<span lang="EN" class="multilang">ANY TEXT TOTO</span>

In short I copy each "EN" web beacon into the same web beacon except I would replace "EN" by "FR".

Example.

At first, my file looks like:

<text><![CDATA[<p><span lang="EN" class="multilang">ANY TEXT TOTO1</span><span lang="FI" class="multilang">ANY TEXT TOTO1</span><span lang="RU" class="multilang">ANY TEXT TOTO1</span></p>]]></text>
<text><![CDATA[<p><span lang="EN" class="multilang">ANY TEXT TOTO2</span><span lang="FI" class="multilang">ANY TEXT TOTO2</span><span lang="RU" class="multilang">ANY TEXT TOTO2</span></p>]]></text>

and should be edited as

text><![CDATA[<p><span lang="FR" class="multilang">ANY TEXT TOTO1</span><span lang="EN" class="multilang">ANY TEXT TOTO1</span><span lang="FI" class="multilang">ANY TEXT TOTO1</span><span lang="RU" class="multilang">ANY TEXT TOTO1</span></p>]]></text>
text><![CDATA[<p><span lang="FR" class="multilang">ANY TEXT TOTO2</span><span lang="EN" class="multilang">ANY TEXT TOTO2</span><span lang="FI" class="multilang">ANY TEXT TOTO2</span><span lang="RU" class="multilang">ANY TEXT TOTO2</span></p>]]></text>
1

Try:

:g/lang="EN"/s%^\(.\{-}\)\(<span .\{-}</span>\)%\1\2\2%|s%lang="EN"%lang="FR"%

Breakdown:

  • g/lang="EN"/ for all lines that contain lang="EN"
  • s%^\(.\{-}\)\(<span .\{-}</span>\)%\1\2\2%
    • ^\(.\{-}\) match the shortest possible prefix
    • \(<span .\{-}</span>\) match from first <span to first </span>
    • \1\2\2 replace with the prefix (first \(...\)) and two times the matched span (second \(...\)).
  • s%lang="EN"%lang="FR"% finally replace first lang="EN" with lang="FR"
  • In fact I have some other web beacon in front of each line containing <span lang="EN". What is the command to match from the first <span> instead the whole beginning of the line ? – Smilia Jan 28 at 14:38
  • 1
    @Smilia Updated – Ralf Jan 28 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.