Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract numbers from string to create digit only string [duplicate]

I have been given some poorly formatted data and need to pull numbers out of strings. I'm not sure what the best way to do this is. The numbers can be any length.

string a = "557222]]>";
string b = "5100870<br>";

any idea what I can do so I'll get this:

a = "557222"
b = "5100870"

Thanks

Solution is for c# sorry. Edited the question to have that tag

like image 661
kevp Avatar asked Jun 12 '12 18:06

kevp


People also ask

How do you extract only digits from a string in Python?

This problem can be solved by using split function to convert string to list and then the list comprehension which can help us iterating through the list and isdigit function helps to get the digit out of a string.

How do you extract digits from a number?

Extracting digits of a number is very simple. When you divide a number by 10, the remainder is the digit in the unit's place. You got your digit, now if you perform integer division on the number by 10, it will truncate the number by removing the digit you just extracted.

How do I only pull numbers from a cell?

Since we have done all the heavy lifting in the code itself, all you need to do is use the formula =GetNumeric(A2). This will instantly give you only the numeric part of the string.


1 Answers

Try this oneliner:

Regex.Replace(str, "[^0-9 _]", "");
like image 133
Milind Raut Avatar answered Oct 19 '22 23:10

Milind Raut