Regex to replace numbers in a string

Hi,

I'd like to remove any number of digits within a string only if the appear at the end of the string. Say that I have strings such as:

Strings
---------------------------
123ABC666
ABC123456
55ABCDE23423423423423423423423FG
FFF12092184928903489238492384982394

Desired output
--------------------------
123ABC
ABC
55ABCDE23423423423423423423423FG
FFF

I can detect if there are numbers at the end of the string using "\d$", and I can replace with $string -replace "[0-9]". However, this wipes out any other numbers anywhere in the string....what I want to do is only wipe out numbers at the end of the string.
if($string -match "\d$") {
     $string -replace "[0-9]"  # Need to target ONLY numbers at the end of string
} else {
     $string
}

Open in new window

LVL 2
bnditAsked:
Who is Participating?
 
SubsunCommented:
$string -replace "[0-9]+$"
$string -replace "\d+$"
0
 
bnditAuthor Commented:
Thx!   what's the "+$" mean? append to the end? only look in the end?
0
 
SubsunCommented:
$ means @End
^ means @Start..
0
 
bnditAuthor Commented:
Thanks!
0
 
SubsunCommented:
Here is a reference..

Regular Expression Cheat Sheet (.NET)
http://regexlib.com/CheatSheet.aspx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.