简体   繁体   中英

Remove all characters except regex pattern in array

So Im creating an array of all the versions of a particular pkg in a directory What I want to do is strip out all the characters except the version numbers The first array has info such as

GoogleChrome.45.45.34.nupkg GoogleChrome.34.28.34.nupkg

So the output I need is 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)'

The last regex matches the pattern of what I want to keep but I cant figure out how to extract what I dont need.

You do not need to post-process matches if you apply the right pattern from the start.

In order to extract . separated digits in between GoogleChrome. and .nupkg you may use

Select-String '(?<=GoogleChrome\.)[\d.]+(?=\.nupkg)' -AllMatches

See the regex demo

Details

  • (?<=GoogleChrome\\.) - the location should be preceded with GoogleChrome. substring
  • [\\d.]+ - one or more digits or/and .
  • (?=\\.nupkg) - there must be .nupkg immediately to the right of the current location.

If .nupkg should not be relied upon, use

Select-String '(?<=GoogleChrome\.)\d+(?:\.\d+)+' -AllMatches

Here, \\d+(?:\\.\\d+)+ will match 1 or more digits followed with 1 or more occurrences of a . and 1+ digits only if preceded with GoogleChrome. .

(\d+.?)+(?!nupkg)

这会在匹配中给你想要的输出,检查正则表达式演示

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM