473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Extract numbers from a string

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
Nov 16 '05 #1
4 18538
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" <we*@balancestudios.com> schrieb im Newsbeitrag
news:eE**************@TK2MSFTNGP10.phx.gbl...
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

Nov 16 '05 #2
will this get numbers with a comma in it? (eg 7,234,609)
"cody" <pl*************************@gmx.de> wrote in message
news:ea**************@TK2MSFTNGP11.phx.gbl...
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" <we*@balancestudios.com> schrieb im Newsbeitrag
news:eE**************@TK2MSFTNGP10.phx.gbl...
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


Nov 16 '05 #3
> 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!

--
Mihai
-------------------------
Replace _year_ with _ to get the real email
Nov 16 '05 #4
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" <we*@balancestudios.com> schrieb im Newsbeitrag
news:Oq*************@TK2MSFTNGP11.phx.gbl...
will this get numbers with a comma in it? (eg 7,234,609)
"cody" <pl*************************@gmx.de> wrote in message
news:ea**************@TK2MSFTNGP11.phx.gbl...
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" <we*@balancestudios.com> schrieb im Newsbeitrag
news:eE**************@TK2MSFTNGP10.phx.gbl...
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



Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Lydia Shawn | last post by:
hi there, i want to extract the numbers from this example input: bla trigger3 trigger4 trigger1 blabla trigger1 5000.00 trigger3 trigger1 trigger2 trigger2 600.00 trigger4 trigger1 50.00...
7
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For...
3
by: Jay Douglas | last post by:
Hello all. I'm having a hard time coming up with a regular expression that can extract just the numbers out of string into a GroupCollection. Any help is appreciated.
3
by: zek2005 | last post by:
Hi friends! I have a varchar field in my DB with numeric values separates by spaces. I need to extract the numbers to create an array. Example 1: 1820 1823 1825 --> need to be transform into ...
3
by: David Moore | last post by:
Hi All, I expect someone can crack this one in no time. Ok, what I'd like to do is to match a pattern in a string and extract a portion of it. For example if I had a string like: 'The store...
6
by: Dave | last post by:
Hope someone can help! I have a memo fiels in which there are a few numbers including dates but what I want to do is extract a number which is 6 figures long. Can anyone help me? Thanks Dave
8
by: Fabian Braennstroem | last post by:
Hi, I would like to remove certain lines from a log files. I had some sed/awk scripts for this, but now, I want to use python with its re module for this task. Actually, I have two different...
0
by: napolpie | last post by:
DISCUSSION IN USER nappie writes: Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file. This file is long file and it's composed by...
5
by: Steve | last post by:
Hi all Does anybody please know a way to extract an Image from a pdf file and save it as a TIFF? I have used a scanner to scan documents which are then placed on a server, but I need to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.