且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用powershell中的正则表达式替换文本文件的内容

更新时间:2023-02-21 12:26:02

是的,您可以在一行中完成,甚至不需要管道,因为 -replace 可以像您一样处理数组希望它这样做(并且您可以链接运算符):

Yes, you can do that in one line and don't even need a pipeline, as -replace works on arrays like you would expect it to do (and you can chain the operator):

(Get-Content Input.json) `
    -replace '"(\d+),(\d{1,})"', '$1.$2' `
    -replace 'second regex', 'second replacement' |
  Out-File output.json

(为了可读性添加了换行符.)

(Line breaks added for readability.)

Get-Content 调用周围的括号是必要的,以防止 -replace 运算符被解释为 Get-Content 的参数.

The parentheses around the Get-Content call are necessary to prevent the -replace operator being interpreted as an argument to Get-Content.