Solved

Regex to replace numbers in a string

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
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…

752 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