powershell 提取两个字符串之间的文本 [英] powershell extract text between two strings
问题描述
我知道以前有人问过这个问题,但我无法得到任何我看过的答案.我有一个包含数千行的 JSON 文件,我想在两个字符串每次出现时简单地提取它们之间的文本(很多).
I know this question has been asked before but I can't get any of the answers I have looked at to work. I have a JSON file which has thousands of lines and want to simply extract the text between two strings every time they appear (which is a lot).
作为一个简单的例子,我的 JSON 看起来像这样:
As a simple example my JSON would look like this:
"customfield_11300": null,
"customfield_11301": [
{
"self": "xxxxxxxx",
"value": "xxxxxxxxx",
"id": "10467"
}
],
"customfield_10730": null,
"customfield_11302": null,
"customfield_10720": 0.0,
"customfield_11300": null,
"customfield_11301": [
{
"self": "zzzzzzzzzzzzz",
"value": "zzzzzzzzzzz",
"id": "10467"
}
],
"customfield_10730": null,
"customfield_11302": null,
"customfield_10720": 0.0,
所以我想输出customfield_11301"和customfield_10730"之间的所有内容:
So I want to output everything between "customfield_11301" and "customfield_10730":
{
"self": "xxxxxxxx",
"value": "xxxxxxxxx",
"id": "10467"
}
],
{
"self": "zzzzzzzzzzzzz",
"value": "zzzzzzzzzzz",
"id": "10467"
}
],
我试图让它尽可能简单 - 所以不要在意输出中显示的括号.
I'm trying to keep it as simple as possible - so don't care about brackets being displayed in the output.
这就是我所拥有的(输出的比我想要的多):
This is what I have (which outputs way more than what I want):
$importPath = "todays_changes.txt"
$pattern = "customfield_11301(.*)customfield_10730"
$string = Get-Content $importPath
$result = [regex]::match($string, $pattern).Groups[1].Value
$result
推荐答案
快速答案是 - 将你的贪婪捕获 (.*)
更改为非贪婪 - (.*?)代码>.应该这样做.
The quick answer is - change your greedy capture (.*)
to non greedy - (.*?)
. That should do it.
customfield_11301(.*?)customfield_10730
否则捕获会吃得尽可能多,导致它一直持续到最后一个customfield_10730
.
Otherwise the capture will eat as much as it can, resulting in it continuing 'til the last customfield_10730
.
问候
这篇关于powershell 提取两个字符串之间的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!