Use Select-String and Regex to extract numbers from text


i have text file following entry:

sometexthere:123

i need extract last 3 digits , assign variable. (the digits change) have far.

ps c:\> $wholetext = "sometexthere:123"

ps c:\> $last3 = $wholetext |select-string -pattern '\d+(?!.*\d+)'

i checked regex pattern , got this:

ps c:\> $wholetext -match '\d+(?!.*\d+)'

true

ps c:\> $matches

name                           value                                                                               
----                           -----                                                                               
0                              123

good far when assign variable whole text string.

ps c:\> $last3

sometexthere:123

what doing wrong?

thanks,

smacdo


hi,

this i'd do:

$str = 'sometexthere:123' $digits = $str.split(':')[-1]  $digits

i avoid regex whenever possible, makes brain hurt.




Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

CRL Revocation always failed

0x300000d errors in Microsoft Remote Desktop client

Failed to query the results of bpa xpath