为什么-replace不能按此powershell脚本中的描述工作?

为什么-replace不能按此powershell脚本中的描述工作?,powershell,Powershell,allacts.txt中的输入文本为: institutionId: blah blah,name: 1st thing,laestabNo: blah blah institutionId: blah blah,name: 2nd thing,laestabNo: blah blah, [lots more things] institutionId: blah blah,name: last thing,laestabNo: blah blah 所需输出为: 1st thing,l

allacts.txt中的输入文本为:

institutionId: blah blah,name: 1st thing,laestabNo: blah blah   institutionId: blah blah,name: 2nd thing,laestabNo: blah blah, [lots more things]   institutionId: blah blah,name: last thing,laestabNo: blah blah
所需输出为:

1st thing,laestabNo: blah blah  2nd thing,laestabNo: blah blah, [lots more things]  last thing,laestabNo: blah blah
我在powershell脚本中使用的命令:

Powershell -NoProfile "(Get-Content -Raw .\allacts.txt) -replace 'institutionid.*,name', '' | Out-File -FilePath allacts.txt -Force -Encoding ASCII"
我实际上得到的是:

last thing,laestabNo:blah blah

没有别的了。我做错了什么?

您的正则表达式不适合您的用例。 你是说

$SomeString = 'institutionId: blah blah,name: 1st thing,laestabNo: blah blah   institutionId: blah blah,name: 2nd thing,laestabNo: blah blah, [lots more things]   institutionId: blah blah,name: last thing,laestabNo: blah blah'
Clear-Host
# RegEx to replace all text string patterns and the text between them
$SomeString -Replace '(institutionId: ).*?(name: )'
# Results
<#
1st thing,laestabNo: blah blah   2nd thing,laestabNo: blah blah, [lots more things]   last thing,laestabNo: blah blah
#>
$SomeString='institutionId:blah blah,name:1st thing,laestabNo:blah institutionId:blah blah,name:2nd thing,laestabNo:blah blah,[Lost more]institutionId:blah blah,name:laestabNo:blah blah blah'
清除主机
#RegEx替换所有文本字符串模式及其之间的文本
$SomeString-Replace'(机构ID:).*(名称:)'
#结果

在正则表达式中,
表示一个字符。
*
表示前一个字符的零个或多个。因此,您告诉它替换从第一个
机构ID
到最后一个
,name
的所有内容。[露齿而笑]看看“贪婪和懒惰的比赛”如何限制这一点。谢谢,是的,我现在明白了。只是去展示一点RTFM有时会比几个小时的敲击键盘要好。你是最受欢迎的!很高兴能帮上一点忙。。。[咧嘴笑]