使用 PowerShell 替换部分字符串 [英] Partial String Replacement using PowerShell
问题描述
问题
我正在编写一个让用户提供特定 IP 地址的脚本,我想以某种方式屏蔽此 IP,以便它不会存储在日志中.我的问题是,当我知道 IP 的前三个值通常是什么时,我可以轻松地做到这一点;但是,如果可能的话,我想避免将这些值存储/硬编码到代码中.即使我不知道前三个值,我也希望能够替换这些值.
示例:
10.11.12.50 将显示为 XX.XX.XX.5010.12.11.23 也会显示为 XX.XX.XX.23
我已经查找了部分字符串替换,但我发现的问题或问题都没有接近于这样做.我试过做这样的事情:
# 这最终替换了所有数字$tempString = $str -replace '[0-9]', 'X'
我知道我已经到了一半,但我的目标是只替换前 3 组数字,所以基本上是."之前的每个数字,但我一直没能做到这一点.
>问题
我正在尝试使用 PowerShell 可以实现吗?是否有实现这一目标的最佳实践方法?
TheIncorrigible1 的有用答案 是一种准确的解决方法问题(仅当 3 个连续的 .
分隔的 1-3 位数字组匹配时才会发生替换.)
一个更宽松但更短的解决方案,它取代了最后一个 .
-前缀的数字组:
PS>'10.11.12.50' -replace '.+(?=\.\d+$)', 'XX.XX.XX'XX.XX.XX.50
(?=\.\d+$)
是(正)先行断言 ((?=...)
) 匹配封闭的子表达式(文字 .
后跟 1或更多数字 (\d
) 在字符串的末尾 ($
)),但不会将它捕获作为整体的一部分匹配.
最终效果是,只有 .+
捕获的内容 - 前瞻断言匹配 之前 的所有内容 - 被替换为 'XX.XX.XX'代码>.
应用于上述示例输入字符串,10.11.12.50
:
(?=\.\d+$)
匹配以.
结尾的数字组,.50
..+
匹配所有before.50
,也就是10.11.12
.>由于
(?=...)
部分未被捕获,因此它不包含在 replaced 中,因此它只是子字符串10.11.12
被替换,即使用XX.XX.XX
,结果产生XX.XX.XX.50
.>
Problem
I am working on a script that has a user provide a specific IP address and I want to mask this IP in some fashion so that it isn't stored in the logs. My problem is, that I can easily do this when I know what the first three values of the IP typically are; however, I want to avoid storing/hard coding those values into the code to if at all possible. I also want to be able to replace the values even if the first three are unknown to me.
Examples:
10.11.12.50 would display as XX.XX.XX.50
10.12.11.23 would also display as XX.XX.XX.23
I have looked up partial string replacements, but none of the questions or problems that I found came close to doing this. I have tried doing things like:
# This ended up replacing all of the numbers
$tempString = $str -replace '[0-9]', 'X'
I know that I am partway there, but I aiming to only replace only the first 3 sets of digits so, basically every digit that is before a '.', but I haven't been able to achieve this.
Question
Is what I'm trying to do possible to achieve with PowerShell? Is there a best practice way of achieving this?
TheIncorrigible1's helpful answer is an exact way of solving the problem (replacement only happens if 3 consecutive .
-separated groups of 1-3 digits are matched.)
A looser, but shorter solution that replaces everything but the last .
-prefixed digit group:
PS> '10.11.12.50' -replace '.+(?=\.\d+$)', 'XX.XX.XX'
XX.XX.XX.50
(?=\.\d+$)
is a (positive) lookahead assertion ((?=...)
) that matches the enclosed subexpression (a literal .
followed by 1 or more digits (\d
) at the end of the string ($
)), but doesn't capture it as part of the overall match.
The net effect is that only what .+
captured - everything before the lookahead assertion's match - is replaced with 'XX.XX.XX'
.
Applied to the above example input string, 10.11.12.50
:
(?=\.\d+$)
matches the.
-prefixed digit group at the end,.50
..+
matches everything before.50
, which is10.11.12
.Since the
(?=...)
part isn't captured, it is therefore not included in what is replaced, so it is only substring10.11.12
that is replaced, namely withXX.XX.XX
, yieldingXX.XX.XX.50
as a result.
这篇关于使用 PowerShell 替换部分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!