Changes to an XML file, please reply quick

I have an XML file with the content example as follows:

<Name="Alan" Area="New-York,New-Jersey,New-Haven,New-Jersey-City,New-York2"/>

What I want to do is to search for "-Jersey" and add -Yes to the above line so that it becomes as:

<Name="Alan" Area="New-York,New-Jersey,New-Jersey-Yes,New-Haven,New-Jersey-City,New-Jersey-City-Yes,New-York2"/>

or adding in the end is also fine

<Name="Alan" Area="New-York,New-Jersey,New-Haven,New-Jersey-City,New-York2,New-Jersey-Yes,New-Jersey-City-Yes"/>

please advise and please let me know if you have any questions.

Thanks.

  • Here is one solution:

    $text = '<Name="Alan" Area="New-York,New-Jersey,New-Haven,New-Jersey-City,New-York2"/>'
    $text -replace ',(.*?Jersey.*?),', ',$1,$1-Yes,' 

    -replace takes a regular expression. The part in parenthesis then can be used in the replacement term as $1 (this is NOT a PowerShell variable, it is a RegEx back reference, so you MUST use single quotes!!)