trim string based on first numeric character

Welcome Forums General PowerShell Q&A trim string based on first numeric character

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years, 2 months ago.

  • Author
  • #5138

    Points: 0
    Rank: Member

    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!


    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!

The topic ‘trim string based on first numeric character’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort