Parsing string

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Parsing string

Post by sekou2331 »

I know this may seem simple but i am trying to parse a string "NP Softwarename.4.8.9.msi. I am trying to just get the numbers but keep the periods so it will look like this "4.8.9". Below is what i have so far. I just need to get rid of the period in the front of the numbers.



  1. $versionNumber = "NP Softwarename.4.8.9.msi"
  2. $a = $versionNumber.Split([array]".msi", [System.StringSplitOptions]::RemoveEmptyEntries) -replace '([a-z])'
.4.8.9
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Parsing string

Post by jvierra »

"NP Softwarename.4.8.9.msi" -match '\d\.\d\.\d'
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Parsing string

Post by sekou2331 »

When i run your line it just says true. If i do a replace it gives everything except the numbers. Not to good at regex so i don't understand whats going on. Will try to read but how do i just get "4.8.9"
  1. $versionNumber = "NP Softwarename.4.8.9.msi"
  2. $versionNumber -replace '\d\.\d\.\d'
NP Softwarename..msi
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Parsing string

Post by jvierra »

  1. PS D:\scripts> "NP Softwarename.4.8.9.msi" -match '\d\.\d\.\d'
  2. True
  3. PS D:\scripts> $matches
  4.  
  5. Name                           Value
  6. ----                           -----
  7. 0                              4.8.9
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Parsing string

Post by sekou2331 »

Ok after messing around with regex I wrote this /(\d+)\.(?:(\d+)\.(\d+))?(?:\-(\w+))?/i. it works for the below in regex but i cant seem to get it to work in powershell. It takes only the numbers and not the leading and trailing periods.


NP Softwarename.4.8.9.msi
NP Softwarename.4.8.msi
NP Softwarename.4.8.90.msi
NP Softwarename.4.msi
NP Softwarename.4.81.9.msi
NP Softwarename.4.8.90523.msi
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Parsing string

Post by jvierra »

You are working it too hard. Just do this:

"NP Softwarename.4.8.9321.msi" -match '\d+\.\d+\.\d+';$matches

(fixed typo)
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Parsing string

Post by sekou2331 »

Is regex the right route to go to get just the numbers below?


NP Softwarename.4.8.9.msi
NP Softwarename.4.8.msi
NP Softwarename.4.8.90.msi
NP Softwarename.4.msi
NP Softwarename.4.81.9.msi
NP Softwarename.4.8.90523.msi
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Parsing string

Post by jvierra »

My example works for all of those examples. except for

We can modify:
"NP Softwarename.4.8.9321.msi" -match '\d+\.\d+\.\d+|\d+\.\d+|\d+';$matches

My original had a typo.
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Parsing string

Post by dan.potter »

If you're really bad at regex, as I am :D .

'NP Softwarename.4.8.9.msi'.substring('NP Softwarename.4.8.9.msi'.indexOf(".") + 1) -replace '.msi'

Makes more sense when you pass the string in as a variable.
User avatar
rmckay9969
Posts: 63
Last visit: Fri Feb 01, 2019 6:51 pm

Re: Parsing string

Post by rmckay9969 »

Or even a slightly shorter regex

\d.*(?=\.\w)

Positive and negative lookaheads are your friend, and powershell understands them just fine. :D
This topic is 7 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked