【发布时间】:2022-01-16 12:55:54
【问题描述】:
我有一个字符串:
[33m[TEST][90m [93ma wonderful testorius line[90m ([37mbite me[90m) which ends here.
您看不到它(因为当我发布它时,stackoverflow 会删除它)但是每个 [xxm 之前都有一个特殊的 Unicode 字符,其中 xx 是一个变量号,[ 以及 @987654326 @ 是固定的。您可以在这里找到特殊字符:https://gist.githubusercontent.com/mlocati/fdabcaeb8071d5c75a2d51712db24011/raw/b710612d6320df7e146508094e84b92b34c77d48/win10colors.cmd
所以,它是这样的(特殊字符在这里用$显示):
$[33m[TEST]$[90m $[93ma wonderful testorius line$[90m ($[37mbite me$[90m) which ends here.
现在,我想删除此行中的所有 $[xxm 子字符串,因为它仅用于彩色监视器输出,但不应保存到日志文件中。
所以预期的结果应该是:
[TEST] a wonderful testorius line (bite me) which ends here.
我尝试使用 RegEx,但我不理解它(可能由于特殊的字符和开放的括号而更加令人困惑)并且我无法在正常的 .Replace ("this","with_that") 操作中使用通配符。
我怎样才能做到这一点?
【问题讨论】:
-
.Replace( )字符串方法不知道regex是什么,需要使用-replace操作符或者直接调用[regex]::Replace( ) -
好的,但我无法理解 RegEx 并且已经尝试过在线生成器。也许我的测试字符串太混乱了。我应该输入哪个表达式?
-
尚不清楚文本的字面含义以及您的预期结果。
-
我都添加了。 $ 代表特殊字符。
标签: regex powershell replace