Replace [fields] on word htm file : how to ?

Daoust ICT
Daoust ICT used Ask the Experts™
I have a htm file created with Word.
This file contains a table (see attached). It's in french. no worry. the purpose is to replace [fields] to real values.
To achieve this, my code is this :
$Datas =  (Select-Xml -Path $strFullFile -XPath //body/h4).node.InnerText}

            $datas = $Datas -replace "[EndOfContract]", $DateFrom
            $datas = $Datas -replace "[TypeOfJcDept]", $strMailboxType
            $datas = $Datas -replace "[JcDEPTName]", $strMailboxAlias
en so forth

this does not seems to work as the code seems to completely destroy the htm file.

If I analyze te situation if each letter of [EndOfCOntract] with is seen on the file is replaced. I was expecting the ONLY [EndOFContrat] Beeing replaced by $dateFrom.

so seems that the command is totally wrong....
what else?
any idea? other method to achieve same result?

Many tks.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jose Gabriel Ortega CastroTop Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018

can you attach a dummy html file with the fields? because it's required to do the testing of the code.

Top Expert 2014

The issue is that the -replace operator uses regex to search for matching text, and brackets ("[" and "]") have a special meaning in regex.  The following would all work correctly.
$datas = $Datas -replace "\[EndOfContract]", $DateFrom
$datas = $Datas -replace "\[TypeOfJcDept]", $strMailboxType
$datas = $Datas -replace "\[JcDEPTName]", $strMailboxAlias

Open in new window

For those, I've just escaped (with a backslash, "\") the opening bracket.  You could escape the closing bracket as well, but with the opening bracket escaped, the closing bracket is interpreted literally without any special significance.  You can read here for more info on regex.

If you have trouble with remembering which characters need to be escaped to be interpreted literally when searching for a match, you can use something like the following.  The [regex]::escape() static method automatically escapes any characters that could have special significance.
$field = "[EndOfContract]"
$regexpattern = [regex]::escape($field)
$datas = $Datas -replace $regexpattern, $DateFrom

Open in new window

Daoust ICTIcT service Desk


Thanks. I have seen indeed that having put my fieldname in [], means for Powershell replace that I'm expecting that every char inside [] need to be replaced ...
I have now suppress those [] and it work's perfectly.

PS Even replacing [] by () does something else. It's replacing string inside () but not the()...

Thanks !
Top Expert 2014
Yes, parentheses also have a special meaning in regex, and again you can escape them with a backslash if you want to do a literal match against them.  The [regex]::escape() method I mentioned previously will handle this as well.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial