Regex Help replace characters between two words

Hi

I need to replace the "&" Character with "&". (Powershell) The difficulty is that I only need to replace between (and include)  "<ItemRoot>" and "</ItemRoot>"

Here is an example:

<Configurations>
      <Configuration>
            <Name>test</Name>
            <Properties>
                  <ItemRoot>
                  <Entry><PropertyId>0</PropertyId><PropertyName>Connection Type</PropertyName><
                  PropertyType>4</PropertyType><PropertyValue>Normal</PropertyValue></Entry>
                  </ItemRoot>
            </Properties>
      </Configuration>
            <Configuration>
            <Name>test</Name>
            <Properties>
                  <ItemRoot>
                  <Entry><PropertyId>0</PropertyId><PropertyName>Connection Type</PropertyName><
                  PropertyType>4</PropertyType><PropertyValue>Normal</PropertyValue></Entry>
                  </ItemRoot>
            </Properties>
      </Configuration>
</Configurations>      


you can see that the "Itemroot" tags can occurs more than one time.....

Any help greatly appreciated.
Server_InfrastrukturAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Server_InfrastrukturAuthor Commented:
Powershell language added
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Can you show the expected result for the first match, so we can be sure about it?
Server_InfrastrukturAuthor Commented:
Result: It should in total 48 replacements (2x24 for each block):

<Configurations>
       <Configuration>
             <Name>test</Name>
             <Properties>
                   <ItemRoot>
                   <Entry><PropertyId>0</PropertyId><PropertyName>Connection Type</PropertyName><
                   PropertyType>4</PropertyType><PropertyValue>Normal</PropertyValue></Entry>
                   </ItemRoot>
             </Properties>
       </Configuration>
             <Configuration>
             <Name>test</Name>
             <Properties>
                   <ItemRoot>
                   <Entry><PropertyId>0</PropertyId><PropertyName>Connection Type</PropertyName><
                   PropertyType>4</PropertyType><PropertyValue>Normal</PropertyValue></Entry>
                   </ItemRoot>
             </Properties>
       </Configuration>
 </Configurations>
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
I'm storing the XML in a single string as $xml:
$xml = @"
<Configurations>
      <Configuration>
            <Name>test</Name>
            <Properties>
                  <ItemRoot>
                  <Entry><PropertyId>0</PropertyId><PropertyName>Connection Type</PropertyName><
                  PropertyType>4</PropertyType><PropertyValue>Normal</PropertyValue></Entry>
                  </ItemRoot>
            </Properties>
      </Configuration>
            <Configuration>
            <Name>test</Name>
            <Properties>
                  <ItemRoot>
                  <Entry><PropertyId>0</PropertyId><PropertyName>Connection Type</PropertyName><
                  PropertyType>4</PropertyType><PropertyValue>Normal</PropertyValue></Entry>
                  </ItemRoot>
            </Properties>
      </Configuration>
</Configurations>
"@

Open in new window

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
The procedural approach, which is not optimal, but easy to understand:
$text = $xml -split '</?ItemRoot>'
for ($i=0; $i -le $text.count; $i++)
{
  if ($i % 2)  {
    $result += '<ItemRoot>' + ($text[$i] -replace '&', '&') + '</ItemRoot>'
  } else {
    $result += $text[$i]
  }
}
$result

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Server_InfrastrukturAuthor Commented:
Thank you very much! I will test it and report back
Server_InfrastrukturAuthor Commented:
Thank's again

At the end it adds "<ItemRoot></ItemRoot>" , but the last few characters should be "......</Properties>
      </Configuration>
</Configurations>"


Otherwise the script is working fine....
Server_InfrastrukturAuthor Commented:
o.k

I found out:
for ($i=0; $i -lt $text.count; $i++)

->lt instead of le
Thank's for your solution. Simple and without great and complicated regex....

Andy
Server_InfrastrukturAuthor Commented:
Between A  and B. Simple solution. Minor thinks to adust.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.