PowerShell-在字符串中替换多个5位子字符串
我们有客户在支付发票时不会提及我们的Inv-Prefix,因此我想将其添加到我们的MT940-BANK语句文件中。
:20 :: 25:Mhaznl2axxx/ 0376603160:28C:102/:60F:C220525EUR5000,
:61:2204130413C336,52NMSCTOPF2500472627 // GBBR001SCT TOPF2500472627
:86:17136 17364 17382 032022102402 WONG TONG SARL 1 MusomethingParis 未提供Sogefrppxxx
:61:2204200420C406,02NMSCTOPF2500479378 // GBCJ005SCT TOPF2500479378
:86:17486 17586 17697 17813 0320221054201 Wong Tong Sarl 1rue 某种东西sogefrppxxx未经证实
我需要它
:20 :: 25:Mhaznl2axxx/ 0376603160:28C:102/:60F:C220525EUR5000,
:61:2204130413C336,52NMSCTOPF2500472627 // GBBR001SCT TOPF2500472627
:86: Inv 17136 Inv 17364 Inv 17382 032022102402 Wong Tong Sarl 1 MusomethingParis sogefrppxxx未提供
:61:2204200420C406,02NMSCTOPF2500479378 // GBCJ005SCT TOPF2500479378
:86: Inv 17486 Inv 17586 Inv 17697 Inv 17813 0320221054201 Wong Tong Sarl 1 rue sosings paris sogefrppxxx不提供
我使用开关语句匹配其他行,这就是为什么我使用开关的原因。
switch -Regex -File c:\Temp\WONG.ged {
':86:.+WONG TONG.+' { $_.replace('([1234567]\d{4}[ ])', "INV$1") }
default { $_ } # unrelated line, pass through
}
这似乎
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有几个问题:
-replace
支持正则替换操作的操作员,而不是string.replace.replace
method(请参阅$
,因此要么在替换周围使用单个引号,'Inv $ 1'
,还是逃脱了backtick,“ Inv` $ 1”
。这将产生预期的结果:
请注意,该模式看起来有些不同:
[1-7]
比冗长的[1234567]
更短且更精细,无需用捕获组将整个模式封闭,您可以使用$&
BackReference参考整个匹配。另外,无需在正则末尾添加+
。还有一个替代解决方案:
请参阅 regex demo 。 详细信息:
(\ g(?!! (
\ g(?!使用
:86:
,除线馈电char以外的任何一个或多个字符,然后尽可能多,然后won tong
string(。*?) - 组2(
$ 2
):除线馈电char以外的任何零或更多字符([1-7] \ d {4}(?!\ s) )
- 第3组($ 3
):1
到7
的数字,然后绳子或立即带有白色空间。(get-content $ filepath)
逐行读取文件,因此任何不匹配的行都会像以前那样输出,不受影响。There are a couple of issues here:
-replace
operator that supports regex replace operation, not theString.Replace
method (see What's the difference between .replace and -replace in powershell?)WING SENG
, so there is no match$
, so either use single quotes around the replacement,'INV$1'
, or escape the backtick,"INV`$1"
.This will yield the expected result:
Note the pattern looks a bit different:
[1-7]
is shorter and leaner than a verbose[1234567]
and there is no need to enclose the whole pattern with a capturing group, you can refer to the whole match with the$&
backreference. Also, there is no need to add.+
at the end of the regex.There is an alternative solution:
See the regex demo. Details:
(\G(?!^)|^(?=:86:.+WONG TONG))
- Group 1: either the end of the previous successful match (\G(?!^)
) or (|
) a string (here, line) start position (^
) that is immediately followed with:86:
, any one or more chars other than line feed char, as many as possible, and thenWON TONG
string(.*?)
- Group 2 ($2
): any zero or more chars other than line feed chars as few as possible([1-7]\d{4}(?!\S))
- Group 3 ($3
): a digit from1
to7
and then any four digits that are at the end of string or immediately followed with a whitespace.(Get-Content $filepath)
reads the file line by line, so any lines that are not matched will be output as is, unaffected.