Solved

Regex to replace numbers in a string

Posted on 2013-01-25
5
2,065 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now