Regex to replace string in a filename found in XML

Asked by
I have a string similar to the following which I am reading from a file into a string:

<ns0:PromptPlaybackComponent x:Name="Intromessage" 
 PromptList="&lt;?xml version=&quot;1.0&quot;?&gt;&#xD;&#xA;&lt;ArrayOfPrompt 
 xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
 xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;&#xD;&#xA;
 &lt;Prompt xsi:type=&quot;AudioFilePrompt&quot;&gt;&#xD;&#xA;
 &lt;Name&gt;AudioFilePrompt1&lt;/Name&gt;&#xD;&#xA;
 &lt;AudioFileName&gt;es_filetoplay.wav&lt;/AudioFileName&gt;&#xD;&#xA;
 &lt;/Prompt&gt;&#xD;&#xA;&lt;/ArrayOfPrompt&gt;"/>
I need to locate and replace es in es_filetoplay.wav with it as in it_filetoplay.wav or whatever the filename is. I only know it ends with .wav.

So the string above after processing should look like:

<ns0:PromptPlaybackComponent x:Name="Intromessage"
PromptList="&lt;?xml version=&quot;1.0&quot;?&gt;&#xD;&#xA;&lt;ArrayOfPrompt
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;&#xD;&#xA;
&lt;Prompt xsi:type=&quot;AudioFilePrompt&quot;&gt;&#xD;&#xA; 
&lt;Name&gt;AudioFilePrompt1&lt;/Name&gt;&#xD;&#xA; 
&lt;AudioFileName&gt;it_filetoplay.wav&lt;/AudioFileName&gt;&#xD;&#xA; 
&lt;/Prompt&gt;&#xD;&#xA;&lt;/ArrayOfPrompt&gt;"/>
Any ideas how to do this with a regular expression?

Best Answer

Answer by
Something like?

var regex = new Regex("(?<=&lt;AudioFileName&gt;)es(?=_.*?\.wav&lt;/AudioFileName&gt;)");
var result = regex.replace(source, "it");
or if you don't know its es and it could be something else

var regex = new Regex("(?<=&lt;AudioFileName&gt;)..(?=_.*?\.wav&lt;/AudioFileName&gt;)");
var result = regex.replace(source, "it");

Your Answer

Name:
Answer: