英文:
Why this Powershell Regex for serial numbers returns everything with Select-String?
问题
I'm trying to get the first serial number of Chromedriver from this string in Powershell with Select-String, but the regex returns everything: '\d{1,4}.\d{1,4}.\d{1,4}.\d{1,4}'.
The regex works well in regex101 and regexr and I've been checking other Powershell regex examples, even this guide. But the shell returns the whole string:
'{"timestamp":"2023-08-04T13:08:44.174Z","channels":{"Stable":{"channel":"Stable","version":"115.0.5790.170","revision":"1148114"},"Beta":{"channel":"Beta","version":"116.0.5845.62","revision":"1160321"},"Dev":{"channel":"Dev","version":"117.0.5911.2","revision":"1175078"},"Canary":{"channel":"Canary","version":"117.0.5929.0","revision":"1179256"}}}'
Why doesn't it return the first serial number "115.0.5790.170"?
The original string can be found here: https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions.json.
英文:
I'm trying to get the first serial number of Chromedriver from this string in Powershell with Select-String, but the regex returns everything: '\d{1,4}.\d{1,4}.\d{1,4}.\d{1,4}'.
The regex works well in regex101 and regexr and I've been checking other Powershell regex examples, even this guide. But the shell returns the whole string
'{"timestamp":"2023-08-04T13:08:44.174Z","channels":{"Stable":{"channel":"Stable","version":"115.0.5790.170","revision":"1148114"},"Beta":{"channel":"Beta","version":"116.0.5845.62","revision":"1160321"},"Dev":{"channel":"Dev","version":"117.0.5911.2","revision":"1175078"},"Canary":{"channel":"Canary","version":"117.0.5929.0","revision":"1179256"}}}' | Select-String -Pattern '\d{1,4}\.\d{1,4}\.\d{1,4}\.\d{1,4}
Why doesn't return the first serial number "115.0.5790.170"?
The original string can be found here.
答案1
得分: 2
正则表达式正常工作,只是您遗漏了__matched value__的展开:
'{"timestamp":"2023-08-04T13:08:44.174Z","channels":{"Stable":{"channel":"Stable","version":"115.0.5790.170","revision":"1148114"},"Beta":{"channel":"Beta","version":"116.0.5845.62","revision":"1160321"},"Dev":{"channel":"Dev","version":"117.0.5911.2","revision":"1175078"},"Canary":{"channel":"Canary","version":"117.0.5929.0","revision":"1179256"}}}' |
Select-String -Pattern '\d{1,4}\.\d{1,4}\.\d{1,4}\.\d{1,4}' |
ForEach-Object { $_.Matches.Value } # 输出:115.0.5790.170
退一步来看,没有必要使用正则表达式,因为您有一个有效的 JSON 字符串,可以使用 ConvertFrom-Json
,然后可以使用点表示法:
$json = $string | ConvertFrom-Json
$json.channels.Stable.version # 输出:115.0.5790.170
英文:
The regex is working properly just that you're missing the expansion of the matched value:
'{"timestamp":"2023-08-04T13:08:44.174Z","channels":{"Stable":{"channel":"Stable","version":"115.0.5790.170","revision":"1148114"},"Beta":{"channel":"Beta","version":"116.0.5845.62","revision":"1160321"},"Dev":{"channel":"Dev","version":"117.0.5911.2","revision":"1175078"},"Canary":{"channel":"Canary","version":"117.0.5929.0","revision":"1179256"}}}' |
Select-String -Pattern '\d{1,4}\.\d{1,4}\.\d{1,4}\.\d{1,4}' |
ForEach-Object { $_.Matches.Value } # Outputs: 115.0.5790.170
Taking a step back, there is no reason to use regex for this when what you have is a valid Json string, use ConvertFrom-Json
and then you can use dot notation:
$json = $string | ConvertFrom-Json
$json.channels.Stable.version # Outputs: 115.0.5790.170
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论