We help IT Professionals succeed at work.

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

Fr. Vandecan
Fr. Vandecan asked
on
110 Views
Last Modified: 2018-01-01
Dear,
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.
Comment
Watch Question

Jose Gabriel Ortega CastroTop-Rated Plus Freelancer (Upwork)/EE Solution Guide / CEO / Photographer
CERTIFIED EXPERT
Awarded 2018
Distinguished Expert 2018

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

thanks
CERTIFIED EXPERT
Top Expert 2014

Commented:
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.  https://www.regular-expressions.info/quickstart.html

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

Fr. VandecanTechnology Sweeper

Author

Commented:
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 !
CERTIFIED EXPERT
Top Expert 2014
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions