bndit
asked on
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
55ABCDE2342342342342342342 3423FG
FFF12092184928903489238492 384982394
Desired output
--------------------------
123ABC
ABC
55ABCDE2342342342342342342 3423FG
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.
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
55ABCDE2342342342342342342
FFF12092184928903489238492
Desired output
--------------------------
123ABC
ABC
55ABCDE2342342342342342342
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
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
$ means @End
^ means @Start..
^ means @Start..
ASKER
Thanks!
ASKER