-2

For example if the string is blahblah02baboon - I need to get the "baboon" seperated from the rest and the variable would countain only the characters "baboon". Every string i need to do this with has alphabet characters first then 2 numbers then more alphabet characters, so it should be the same process everytime.

Any advice would be greatly appreciated.

IanB
  • 271
  • 3
  • 13
  • 1
    Please read [ask] and post a [mcve]. Show us your code, what you tried, where it went wrong. – briantist Jan 22 '18 at 00:43
  • 2
    `$text = "blahblah02baboon"; $from = $text.LastIndexOfAny("01234567890".ToCharArray()); $lastWord = $text.Substring($from + 1); Write-Host $lastWord;` – derloopkat Jan 22 '18 at 16:43

2 Answers2

1

My advice is to learn about regular expressions.

'blahblah02baboon' -replace '\D*\d*(\w*)', '$1'
0

Or use regex

$MyString = "01baaab01blah02baboon"

# Match any character which is not a digit
$Result = [regex]::matches($MyString, "\D+")

# Take the last result
$LastResult = $Result[$Result.Count-1].Value

# Output
Write-Output "My last result = $LastResult"
Glenn G
  • 146
  • 3