英文:
get text inside a section and remove the empty section in PowerShell
问题
以下是您要翻译的部分:
我有一个文件,内容如下:
```none
parallel (
{
some content 1
some content 2
build ( 'Job3', parameter5: value5)
some content 3
some content 4
}
)
parallel (
{
some content 5
some content 6
build ( 'Job2', parameter3: value3, parameter4: value4)
}
)
build ( 'Job1', parameter1: value1, parameter2: value2)
我需要保留仅位于parallel({})内的内容,并从文件中删除parallel({})部分。
注意:棘手的部分是t
预期输出
some content 1
some content 2
build ( 'Job3', parameter5: value5)
some content 3
some content 4
some content 5
some content 6
build ( 'Job2', parameter3: value3, parameter4: value4)
build ( 'Job1', parameter1: value1, parameter2: value2)
<details>
<summary>英文:</summary>
I have a file with below content
```none
parallel (
{
some content 1
some content 2
build ( 'Job3', parameter5: value5)
some content 3
some content 4
}
)
parallel (
{
some content 5
some content 6
build ( 'Job2', parameter3: value3, parameter4: value4)
}
)
build ( 'Job1', parameter1: value1, parameter2: value2)
I need to keep only the content which is inside parallel ({ }) and remove the parallel({ }) section from the file.
Note: The tricky part is t
Expected output
some content 1
some content 2
build ( 'Job3', parameter5: value5)
some content 3
some content 4
some content 5
some content 6
build ( 'Job2', parameter3: value3, parameter4: value4)
build ( 'Job1', parameter1: value1, parameter2: value2)
答案1
得分: 0
你可以使用正则表达式进行解析,详细信息请参见 https://regex101.com/r/DiDgli/1。
$re = [regex] '(?mi)(?<=parallel\(\s*\{)(?:\s*\b([^\r\n]+))+|(^build[^)]+\))'
$re.Matches((Get-Content path\to\file.txt -Raw)) | ForEach-Object {
if ($_.Groups[1].Success) {
return $_.Groups[1].Captures.Value
}
$_.Groups[2].Value
}
英文:
You can parse it with regex, see https://regex101.com/r/DiDgli/1 for details.
$re = [regex] '(?mi)(?<=parallel\s*\(\s*\{)(?:\s*\b([^\r\n]+))+|(^build[^)]+\))'
$re.Matches((Get-Content path\to\file.txt -Raw)) | ForEach-Object {
if ($_.Groups[1].Success) {
return $_.Groups[1].Captures.Value
}
$_.Groups[2].Value
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论