Solved

Regex to replace numbers in a string

Posted on 2013-01-25
5
2,218 Views
Last Modified: 2013-01-25
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

0
Comment
Question by:bndit
  • 3
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 205 total points
ID: 38821043
$string -replace "[0-9]+$"
$string -replace "\d+$"
0
 
LVL 2

Author Comment

by:bndit
ID: 38821046
Thx!   what's the "+$" mean? append to the end? only look in the end?
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38821055
$ means @End
^ means @Start..
0
 
LVL 2

Author Closing Comment

by:bndit
ID: 38821061
Thanks!
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38821072
Here is a reference..

Regular Expression Cheat Sheet (.NET)
http://regexlib.com/CheatSheet.aspx
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question