Extract numbers from a string

  • Thread starter Thread starter Ryan
  • Start date Start date
R

Ryan

I am trying to write a c# function to extract all numbers out of a string.
What is the easiest way to do this, regular expressions?

I must account for numbers formatted with or without commas (decimal points
are not important).

thnx
 
will this get numbers with a comma in it? (eg 7,234,609)


cody said:
use the regex "[0-9]+" or "\d+" to extract numbers from a given string.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Ryan said:
I am trying to write a c# function to extract all numbers out of a string.
What is the easiest way to do this, regular expressions?

I must account for numbers formatted with or without commas (decimal points
are not important).

thnx
 
will this get numbers with a comma in it? (eg 7,234,609)
No.
decimal points are not important
For this you should be aware of the locales of the data (not of the system).
Because in most european countries, "decimal point is comma" (this was an
old command in the old dinosaur, COBOL).

In Germany you will get 7.234,123

Even worst, in France they use a non-breaking space as thousand separator.
And if the data is input by a human, not produced by a program, there are
big chances he/she will type spaces instead of non-breaking spaces: 7 234,123

And if you don't know the locales for the data, sometimes is impossible to
guess: 3,456. In U.S. this is three thousands four hundreds fifty six.
In Germany, is 3 and almost a half.

If this is about prices, I would not like to do a mistake here!
 
In this case you could use a regex like:

\d+(,\d{3})*

it find all numbers of the form d[d][d]{[,ddd]} within a string.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Ryan said:
will this get numbers with a comma in it? (eg 7,234,609)


cody said:
use the regex "[0-9]+" or "\d+" to extract numbers from a given string.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Ryan said:
I am trying to write a c# function to extract all numbers out of a string.
What is the easiest way to do this, regular expressions?

I must account for numbers formatted with or without commas (decimal points
are not important).

thnx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top