?
Solved

Extract number from text

Posted on 2013-02-06
10
Medium Priority
?
522 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

 
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 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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
 
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 75

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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

770 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