trim string based on first numeric character

by andy12345 at 2012-09-24 04:30:45

Hi All,

I’m trying to remove the non-numeric characters from the start of my string. eg, I may have abc12345 or abcde12345, and in both cases I want to be left with just 12345. Can anyone suggest a way to do this? I was trying to use substring with LastIndexOf, but it doesn’t like wildcards, eg

$mystring = $mystring.substring($msytring.LastIndexOf(‘[a-z]’)+1)

I realise the above doesn’t work, but it hopefully demonstrates what I’m trying to do!

Thanks.
by jonhtyler at 2012-09-24 05:48:13
You could use regular expression here with the -replace operator:

$mystring -replace "^\D+", ""

The regular expression says to start at the beginning (^) and take any non-numeric (\D) characters if there are 1 or more (+) and replace them with an empty string.
by andy12345 at 2012-09-24 06:17:03
Perfect, thank you!