Remko
Remko

Reputation: 7340

RegEx in Powershell to replace space in XML element name with underscore

I have an XML document with element names with a space or multiple spaces in it (which is not allowed in XML) and I am looking for a regex to replace the space with an _ and after modifications replace the _ with a space again. The regex can be applied to a string.

Simplified Sample XML where I want to replace <User Blob> to <User_Blob> but I don't want to replace eg My Space to My_Space. So the RegEx needs to match a < followed by one or more words with a space in it, followed by > I think.

<User Data Blob> 
    <Item> 
        <Key>SomeKey</Key> 
        <Value>false</Value> 
    </Item> 
    <Item> 
        <Key>AnotherKey</Key> 
        <Value></Value> 
    </Item> 
</User Data Blob> 

Upvotes: 1

Views: 1291

Answers (3)

Shay Levy
Shay Levy

Reputation: 126892

Get-Content .\file.xml | Foreach-Object {
    [regex]::replace($_,'<([^>]+)>',{$args[0] -replace ' ','_'})
}

Upvotes: 2

mjsr
mjsr

Reputation: 7590

From Space to Underscore:

(gc .\FileWithSpace.xml)| % { $_ -replace "<(/?)(\w+) (\w+)>", '<$1$2_$3>'} 

From Underscore to Space:

(gc .\FileWithUnderscore.xml)| % { $_ -replace "<(/?)(\w+)_(\w+)>", '<$1$2 $3>'}

Upvotes: 1

Qtax
Qtax

Reputation: 33928

If the regex flavor used supports lookahead you could do things like:

 (?=[<>]*>)

(Notice the space in front.) Replace with _.

To reverse do:

_(?=[<>]*>)

Replaced with a space.

Upvotes: 0

Related Questions