powershell - 为什么 -replace 不能像这个 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,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
#>
推荐阅读
- javascript - toLocaleDateString 从意大利语转换为英语
- anaconda - 尝试更新 conda 但出现错误无法链接不存在的源
- javascript - 第一次通过后循环内容脚本中断 - Chrome 扩展
- python - 运行 model.fit() 时的 tf.keras (RNN) 层问题
- python - 在 Python 2.7 中将 SameSite 参数添加到 cookie
- visual-studio-2019 - 如何在将 SSDT 与 Visual Studio 2019 结合使用时数据库发生更改时更新 SSAS 表格模型?
- webpack - 具有条目依赖的代码拆分但出现错误,我的 webpack 版本是 4.41.6(目前最新)
- javascript - 与 Greensock 反应 - 请 gsap.registerPlugin(CSSPlugin, CSSRulePlugin)
- php - curl使用php将文件从服务器放到扩音器
- python - WebDriverError:断开连接:无法连接到渲染器(python)