Solved

Extract number from text

Posted on 2013-02-06
10
513 Views
Last Modified: 2013-02-06
Hi

Given a number such as:

0824518765

How can I extract that best from some text in c#?

The number will always begin with 0, and is 10 digits long, but there may be spaces or hyphens between
0
Comment
Question by:zattz
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 38859251
does it appear randomly?
can u post some text example?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38859285
you can replace individual characters (strings) and the parse the resulting string eg.

String s = "082 451-87 - 65";
UInt64 l = UInt64.Parse(s.Replace("-", "").Replace(" ", ""));

l is 824518765
0
 

Author Comment

by:zattz
ID: 38859294
The number changes dynamically. I am parsing it out of some text files. I presume a regex is needed.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38859295
ps.  Numerical values do NOT have leading zero's so  0824518765 is not a numerical value

If you wanted the string then using the above example
String s = "082 451-87 - 65";
s = s.Replace("-", "").Replace(" ", "");  //s is 0824518765
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38859330
A regex could be an option:

using System.Text.RegularExpressions;

...

Match m = Regex.Match(targetString, @"\b0(?:[ -]*\d){9}\b");

if (m.Success)
{
    string numericalValue = m.Groups[0].Value;
}

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38859347
>>I presume a regex is needed.

No - see my code earlier.  A regex is an option, not a requirement.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 38859494
@AndyAinscow

Your example is a tad on the trivial side, though. It does not demonstrate the "from some text" aspect of the OP. I'm certain it could be made to do so, but your example doesn't really convey this.
0
 

Author Comment

by:zattz
ID: 38859534
A regex is definitely needed because the number is a phone number, and it changes...
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38859671
@kaufmed
Extract of question:   The number will always begin with 0, and is 10 digits long, but there may be spaces or hyphens between

My example fits the complete stated requirements - it copes with both hyphens and spaces.
0
 

Author Closing Comment

by:zattz
ID: 38859761
Thanks so much
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now