Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2954
  • Last Modified:

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

0
bndit
Asked:
bndit
  • 3
  • 2
1 Solution
 
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

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now