Given some input data:
<somexml>
<User Name="MrFlibble">
<Option Name="Pass">SomeSaltedPassword</Option>
<Option Name="Salt">Salt</Option>
<tag1></tag1>
<Permissions>
<Permission Dir="E:"></Permission>
</Permissions>
</User>
<User Name="MrFlobble">
<Option Name="Pass">SomeOtherSaltedPassword</Option>
<Option Name="Salt">Salt</Option>
<tag1></tag1>
<Permissions>
<Permission Dir="C:"></Permission>
</Permissions>
</User>
</somexml>
I'd like to replace the first user that doesn't have a C:
permission in the user area (in this case MrFlibble
) with Jon
and SomeSaltedPassword
with MyNewSaltedPassword
using a .net framework regex to give the following result:
<somexml>
<User Name="Jon">
<Option Name="Pass">MyNewSaltedPassword</Option>
<Option Name="Salt">Salt</Option>
<tag1></tag1>
<Permissions>
<Permission Dir="E:"></Permission>
</Permissions>
</User>
<User Name="MrFlobble">
<Option Name="Pass">SomeOtherSaltedPassword</Option>
<Option Name="Salt">Salt</Option>
<tag1></tag1>
<Permissions>
<Permission Dir="C:"></Permission>
</Permissions>
</User>
</somexml>
I think something like this regex would capture the users and group the sections I Want to replace:
<User Name="(.*)">.*<Option Name="Pass">(.*)<\/Option>.*<Option Name="Salt">(.*)<\/Option>.*<\/User>
...but I'm struggling to see how I would substitute the three groups while maintaining the other text. The docs all seem to suggest replacing modifications of the original text rather than multiple specifically named groups with specific new text.
Is there a standard way to do this or am I barking up the wrong tree?
Do not under any circumstances try to parse XML with a regex unless you wish to invoke rite 666 Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn.
Use an XML parsing library see this page for some ways to do it.