[英]Remove all characters except regex pattern in array
所以我在一個目錄中創建一個特定pkg的所有版本的數組我想要做的是刪除除版本號之外的所有字符第一個數組有信息,如
GoogleChrome.45.45.34.nupkg GoogleChrome.34.28.34.nupkg
所以我需要的輸出是45.45.34 34.28.34
$dirList = Get-ChildItem $sourceDir -Recurse -Include "*.nupkg" -Exclude
$pkgExclude |
Foreach-Object {$_.Name}
$reg = '.*[0-9]*.nupkg'
$appName ='GoogleChrome'
$ouText = $dirList | Select-String $appName$reg -AllMatches | % {
$_.Matches.Value }
$ouText
$verReg='(\d+)(.)(?!nupkg)'
最后一個正則表達式匹配我想要保留的模式,但我無法弄清楚如何提取我不需要的東西。
如果從一開始就應用正確的模式,則無需后處理匹配。
為了提取.
GoogleChrome.
之間的分隔數字GoogleChrome.
和.nupkg
你可以使用
Select-String '(?<=GoogleChrome\.)[\d.]+(?=\.nupkg)' -AllMatches
請參閱正則表達式演示
細節
(?<=GoogleChrome\\.)
- 該位置應在GoogleChrome.
之前GoogleChrome.
子 [\\d.]+
- 一個或多個數字或/和.
(?=\\.nupkg)
.nupkg
(?=\\.nupkg)
- 當前位置右邊必須有.nupkg
。 如果不應該依賴.nupkg
,請使用
Select-String '(?<=GoogleChrome\.)\d+(?:\.\d+)+' -AllMatches
這里, \\d+(?:\\.\\d+)+
將匹配1個或更多個數字,然后出現1個或多個a .
僅當前面有GoogleChrome.
才有1位數字GoogleChrome.
。
(\d+.?)+(?!nupkg)
這會在匹配中給你想要的輸出,檢查正則表達式演示
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.