1

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.

1
  • Try Select-String '(?<=GoogleChrome\.)\d+(?:\.\d+)+' -AllMatches Commented Aug 22, 2017 at 10:21

2 Answers 2

2

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..

Sign up to request clarification or add additional context in comments.

Comments

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

this would give you desired output in the match, check the regex demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.