0
在下面的代码中,我试图用一个保存正确代码的css文件替换一部分运行不正常的css文件。“正则表达式模式无效”,而用数组替换正则表达式
在第1部分,我获取了这两个文件。
在第2部分,我用正则表达式从bad.css中过滤无效部分。
在第3部分,我想用good.css文件替换坏的css。但是我得到错误“正则表达式模式无效”。
应该注意的是$b
是一种数组,而$d
是一种字符串。但我真的不知道它是否有所作为。
#1. get files
$overrider = (Join-Path $powerShellResourcePath "good.css")
$b = Get-Content $overrider
$stylingOverride = (Join-Path $contentPath "bad.css")
$c = Get-Content $stylingOverride
#2. gets the part that should be replaced in bad.css
$d = [Regex]::Matches($c, '(start)(.*?)(end)') | Select -ExpandProperty Value
#3. should replace the regex part in bad.css with the whole good.css
if ($d) {
$c = $c -replace $d, $b
Set-Content $stylingOverride -Value $c
}
替换时'$ d'中的值是多少?也许你只需要逃避它 - '[正则表达式] :: Escape($ d)'。另外,'start'和'end'是'$ d'的一部分,对吧?也许你需要从中排除分隔符,''(?<= start)。*?(?= end)'' –
'$ d'的值是开始和结束之间的所有内容。我试过'[正则表达式] :: Escape($ d)',它确实修正了错误。然而在CSS文件中没有任何改变。 – 2hTu2
这是因为值中的“开始”和“结束”。试试'匹配($ c,'(?<= start)。*?(?= end)')' –