代码之家  ›  专栏  ›  技术社区  ›  wonea Ilya Smagin

删除HTML中的区域

  •  2
  • wonea Ilya Smagin  · 技术社区  · 8 年前

    我想删除HTML中的自强区域。我已经计算出了正确的regex,我已经在 expresso中证明了这一点,并且突出显示了正确的部分。

    我在多行模式下运行此设置,并已将此设置预先设置为PowerShell Regex字符串

    get childitem(get item-path“.\”-verbose).fullname-recurse-filter*.html|
    foreach对象{
    写主机“检查”$.fullname
    $content=get content$全名
    $content=$content-替换“(?m)(^.*Region Remove.*)[.*\n \w \w]*(^.*EndRegion Remove.)”,“
    设置内容$content-path$.fullname
    }
    

    遗憾的是,该区域未被删除,尽管文件已被触摸。,并突出显示正确的部分。

    Expresso

    我在多行模式下运行此设置,并已将此设置预先设置为PowerShell Regex字符串

    Get-ChildItem (Get-Item -Path ".\" -Verbose).FullName -Recurse -Filter *.html |
    Foreach-Object {
        Write-Host "Checking "$_.FullName
        $content = Get-Content $_.FullName
        $content = $content -replace "(?m)(^.*\#region REMOVE.*)[.|\n|\W|\w]*(^.*\#endregion REMOVE.*)",""
        Set-Content $content -Path $_.FullName
    }
    

    遗憾的是,该区域没有被删除,尽管文件已被触摸。

    1 回复  |  直到 8 年前
        1
  •  2
  •   kuujinbo    8 年前

    来自 Get-Content documentation :

    get-content cmdlet在该位置获取项的内容 由路径指定,例如文件中的文本。它读到 一次内容一行,并返回一组对象,每个对象 其中表示一行内容。

    所以regex是在数组上执行的,而不是文件内容的字符串。更改为:

    $content = [System.IO.File]::ReadAllText($_.FullName);
    

    它会起作用的。

    推荐文章