How to strip out numeric characters only from a string

activities
studio

#1

Hi,

I have a string of “Please bill 28kg” or sometimes “Bill 28lbs” or many more potential type of string that I would receive.

So now the question is how do I just strip out the numeric characters only from the string.

Example:
String - “Bill 28kg
Expecting outcome - “28

Do anyone have any idea how to do this? I’ve tried Regex, but not sure how to do it as the output is in list of array. :frowning:


How to check a 5 digit text?
Convert datacolumns from string to decimal
#2

@Serran_Neru

try this

string b= System.Text.RegularExpressions.Regex.Replace(“string_Variable”,"\D","")

It will replace all the characters other than digits and return only digits.

Regards,
Mahesh


Excel read cell
#3

You’re the start Mahesh :slight_smile:


#4

Hi Mahesh,

The code is really a good one. Unfortunately if the string contain “Please bill 28.4 kg”. It returns as “284”. Meaning to say it also strip out the decimal points. Can anycome please assist me on this?

Regards,
Serran


#5

I got it Mahesh, I’ve used [A-z]+ and it works :slight_smile:


#6

I have similar case. I want to extract just the amount.
However my case is the amount appears: Balance Due CHF2,335.00.

I need to take care of the comma and decimal.

So using above the decimal , comma changes into *, the output appear as *233500


#7

@Mallika

Try like this

string b = System.Text.RegularExpressions.Regex.Replace(“string_Variable”,"[^/./,0-9]","")

Regards,
Mahesh


#8

Great!! Thanks a lot!!


#9

hi @MAHESH1
similar case i have .
if my input is like: 2345 ref number on 2.2.18
the output should get only 2345
can you help on this ?


#10

input.Split(" "c)(0).toString will give your result.


#11

HI … thanks for the reply and even i have found out one solution using regex (\D)([0-9])(.)*


#12