【问题标题】:Remove text+ pattern from txt file从文本文件中删除文本模式
【发布时间】:2015-09-05 02:30:04
【问题描述】:

我正在寻找如何从另一个文件 acc.txt(或 acc.html)中删除 pattern.txt 中指定的文本的解决方案。

<table cellpadding="5" cellspacing="0" border="0" width="100%">
<tr>
<td style="border-bottom-color: #d0d0d0; border-bottom-style: solid; border-bottom-width: 1px; background-color: #eaeaea;"><!-- F1E896 -->
<font style="font-size: 13px;"><b>{.*(everything until meeting <blockquote>}
<blockquote>
{ .{1,5}? (any letters/space characters/tabs -size maximum 5)}
</blockquote>
</td>
</tr>
</table><br>
<br>

应该可以忽略这些字符。我更喜欢使用提示。我知道处理 .html 文件看起来并不那么容易,如果我们将其保存为 txt 会有所不同吗?

编辑:可能适用于

<table {skip everything to first met}<blockquote>{max 5 letters}<blockquote>{skip everyhing to <br>

【问题讨论】:

  • 将文件保存为 .txt 没有任何区别。
  • HTML 是您要删除的模式,还是您希望从中删除模式的文本?另外,到目前为止,您尝试过什么? SO 不是其他人为您编写代码的地方。

标签: regex powershell command-line command prompt


【解决方案1】:

将模式保存在文件中,例如。 G。 "c:\pattern.txt":

(?<=<b>).*(?=<blockquote>)|(?<=<blockquote>).*(?=<\/blockquote>)

使用Get-Content cmdlet 加载模式和文本文件并将其替换为空字符串:

$content = (Get-Content 'c:\acc.txt' -raw) 
$pattern = (Get-Content 'c:\pattern.txt' -raw)

[regex]::Replace($content, $pattern, '',`
     [System.Text.RegularExpressions.RegexOptions]::Multiline `
     -bor [System.Text.RegularExpressions.RegexOptions]::Singleline)

现在您可以将输出通过管道传输到 Out-FileSet-Content cmdlet。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 2021-12-23
    • 2015-12-22
    • 2011-01-05
    相关资源
    最近更新 更多