473,569 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 18549
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*@balancestu dios.com> schrieb im Newsbeitrag
news:eE******** ******@TK2MSFTN GP10.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************ *************@g mx.de> wrote in message
news:ea******** ******@TK2MSFTN GP11.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*@balancestu dios.com> schrieb im Newsbeitrag
news:eE******** ******@TK2MSFTN GP10.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*@balancestu dios.com> schrieb im Newsbeitrag
news:Oq******** *****@TK2MSFTNG P11.phx.gbl...
will this get numbers with a comma in it? (eg 7,234,609)
"cody" <pl************ *************@g mx.de> wrote in message
news:ea******** ******@TK2MSFTN GP11.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*@balancestu dios.com> schrieb im Newsbeitrag
news:eE******** ******@TK2MSFTN GP10.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
4385
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 trigger4
7
3588
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 example, consider this four-bit number: 1100 from this number I want to extract two numbers: 1000 and 100 had the four-bit number been 0101 I...
3
10137
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
3510
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 1820 1823 1825
3
1777
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 had apples(20) and pears(30)' how do I extract the '20' and '30'? ie. if the pattern is 'apples(??)', how do I expect the '??'.
6
5949
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
2820
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 log files. The first file looks like: ...
0
2036
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 word and number arguments like this: GRILLE EURAT5 Coin Nord-Ouest : 46.50/ 0.50 Coin Sud-E Hello, I'm Peter and I'm new in python codying and I'm...
5
5745
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 extract the image of the document (just the first page if there are multiple pages) and save it as a TIFF so I can then use the Tesseract OCR to get the...
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.