PowerShell-在字符串中替换多个5位子字符串

发布于 2025-02-03 23:57:49 字数 1342 浏览 3 评论 0 原文

我们有客户在支付发票时不会提及我们的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
    } 

这似乎

We have customers that do not mention our INV-prefix when they pay invoices so I want to add it to our MT940-bank statement file.

:20: :25:MHAZNL2AXXX/0376603160 :28C:102/ :60F:C220525EUR5000,
:61:2204130413C336,52NMSCTOPF2500472627//GBBR001SCT TOPF2500472627
:86:17136 17364 17382 032022102402 WONG TONG SARL 1 RUESOMETHINGPARIS
SOGEFRPPXXX NOT PROVIDED
:61:2204200420C406,02NMSCTOPF2500479378//GBCJ005SCT TOPF2500479378
:86:17486 17586 17697 17813 0320221054201 WONG TONG SARL 1RUE
SOMETHINGPARIS SOGEFRPPXXX NOTPROVIDED

I need it to be

:20: :25:MHAZNL2AXXX/0376603160 :28C:102/ :60F:C220525EUR5000,
:61:2204130413C336,52NMSCTOPF2500472627//GBBR001SCT TOPF2500472627
:86:INV17136 INV17364 INV17382 032022102402 WONG TONG SARL 1
RUESOMETHINGPARIS SOGEFRPPXXX NOT PROVIDED
:61:2204200420C406,02NMSCTOPF2500479378//GBCJ005SCT TOPF2500479378
:86:INV17486 INV17586 INV17697 INV17813 0320221054201 WONG TONG SARL
1RUE SOMETHINGPARIS SOGEFRPPXXX NOTPROVIDED

I use a switch statement to match other lines as well that is why I use switch.

switch -Regex -File c:\Temp\WONG.ged  {
    ':86:.+WONG TONG.+'   { $_.replace('([1234567]\d{4}[ ])', "INV$1") }
    default               { $_ } # unrelated line, pass through
    } 

This seems to work but the replace does not work (if I replace eg. WONG for TEST then it works fine).
I added 1234567 to prevent it from matching too much but it still matches too much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终陌 2025-02-10 23:57:49

这里有几个问题:

  • 您需要使用 -replace 支持正则替换操作的操作员,而不是 string.replace.replace method(请参阅
  • ://stackoverflow.com/q/10184156/3832970“> .replace和-replace in PowerShell中有什么 匹配
  • 当您定义反向时,您需要使用文字 $ ,因此要么在替换周围使用单个引号,'Inv $​​ 1',还是逃脱了backtick,“ Inv` $ 1”

这将产生预期的结果:

switch -Regex -File c:\Temp\WONG.ged {
 ':86:.+WONG TONG'   { $_ -replace '[1-7]\d{4} ', 'INV
amp;' }
 default             { $_ } # unrelated line, pass through
}

请注意,该模式看起来有些不同: [1-7] 比冗长的 [1234567] 更短且更精细,无需用捕获组将整个模式封闭,您可以使用 $& BackReference参考整个匹配。另外,无需在正则末尾添加+

还有一个替代解决方案:

(Get-Content $filepath) -replace '(\G(?!^)|^(?=:86:.+WONG TONG))(.*?)([1-7]\d{4}(?!\S))', '$1$2INV$3'

请参阅 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:

  • You need to use the -replace operator that supports regex replace operation, not the String.Replace method (see What's the difference between .replace and -replace in powershell?)
  • Your text does not contain WING SENG, so there is no match
  • When you define a backreference, you need to use a literal $, so either use single quotes around the replacement, 'INV$1', or escape the backtick, "INV`$1".

This will yield the expected result:

switch -Regex -File c:\Temp\WONG.ged {
 ':86:.+WONG TONG'   { $_ -replace '[1-7]\d{4} ', 'INV
amp;' }
 default             { $_ } # unrelated line, pass through
}

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:

(Get-Content $filepath) -replace '(\G(?!^)|^(?=:86:.+WONG TONG))(.*?)([1-7]\d{4}(?!\S))', '$1$2INV$3'

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 then WON 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 from 1 to 7 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文