Extract number from text

Posted on 2013-02-06
Last Modified: 2013-02-06

Given a number such as:


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
Question by:zattz
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
LVL 42

Expert Comment

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

Expert Comment

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

Author Comment

ID: 38859294
The number changes dynamically. I am parsing it out of some text files. I presume a regex is needed.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 44

Expert Comment

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
LVL 75

Accepted Solution

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

LVL 44

Expert Comment

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

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

Expert Comment

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

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.

Author Comment

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

Expert Comment

ID: 38859671
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.

Author Closing Comment

ID: 38859761
Thanks so much

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Office 365: Assigning MailboxPlan to a mailbox 8 69
How do i create a simple array in a public class? 6 35
C# LINQ query question 11 30
Error in Visual Project 10 47
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…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

737 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