2017-04-26 6 views
0

`PowerShellを使用してHTMLの特殊文字を含む文字列を置き換える方法はありますか?

Get-Content C:\Users\sfair\Documents\Frontline\report.htm | Out-File C:\Users\sfair\Documents\Frontline\report1.txt 
$editreport = C:\Users\sfair\Documents\Frontline\report1.txt 
$editreport -replace [regex]::Escape('employee(id','employee</th><th>id</th>') 
$editreport -replace [regex]::Escape('employeeType(id)','id') 
$editreport -replace [regex]::Escape('employee(','employee</td><td>') 
$editreport -replace [regex]::Escape(')</td>','</td>') 

` 私はすべての4のために同様のメッセージを取得する操作を置き換える:

Cannot find an overload for "Escape" and the argument count: "2". 
At C:\Users\sfair\Documents\Frontline\still-trying1.ps1:40 char:1 
+ $editreport -replace [regex]::Escape('employee(id','employee</th><th> ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [], MethodException 
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest 

enter image description here 私が.CSVで読んで、PowerShellでHTMLファイルを生成しています。ある文字列を別の文字列に置き換えて、HTMLを編集したいと思います。

例:$データ-replace "従業員(ID"、 "社員"

これは、次のメッセージになり:

正規表現パターン "従業員(ID"、 "社員" が有効ではありません。

+1

あなたのリテラルの入力パターン '$データ-replace [正規表現] ::エスケープ( '従業員(ID')をエスケープする必要があり、」 emplyeeid '' –

+0

ここに私が今使っている線があります: – Steve

答えて

1

正規表現で特殊文字をエスケープするには、バックスラッシュを使用してください。

$data -replace 'employee\(id', 'employeeid' 
関連する問題