Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regex to replace numbers in a string

Posted on 2013-01-25
5
Medium Priority
?
2,763 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 820 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

670 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