Extract digit number from a string

Posted on 2011-10-04
Last Modified: 2012-05-12
Hello, just need an advice.

I have strings, each of them might contain digit number. For example,
string str1 = "abcdet5678sd"; // containg 5678
srting str2 = "dfg34567c"; // containing 2456

Open in new window

I want to extract these numeric values.
Finally we will get
string new_str = "5678"
string new_str = "34567";

Open in new window

The question is that we don't know how long of the digit number and what is the position.
Also it may not have a number in the string, in this case, just give a message.
Thanks for advice.
Question by:zhshqzyc
    LVL 53

    Expert Comment

    using System;
    using System.Data;
    using System.Text.RegularExpressions;
    class Class1{
            static void Main(string[] args){
          string IsNotNum = "111west";
          string IsNum = "  +111  ";
          string IsFloat = "  23.11  ";
          string IsExp = "  +23 e+11  ";
          Console.WriteLine(GetNumberFromStr(IsNum));    // +111
          Console.WriteLine(GetNumberFromStr(IsNotNum));  // 
          Console.WriteLine(GetNumberFromStr(IsFloat));  // 23.11
          Console.WriteLine(GetNumberFromStr(IsExp));    // 
        public static string GetNumberFromStr(string str)
          str = str.Trim();
          Match m = Regex.Match(str, @"^[\+\-]?\d*\.?[Ee]?[\+\-]?\d*$");
          return (m.Value);

    Open in new window


    Author Comment

    Well, the code will get "+111" from "111west". But I only want to get "111".
    And if the original string pattern likes "abc111west", I suspect whether it works or not?
    LVL 53

    Expert Comment

    Try the following:
    string str = "abc111west";
    str = string.Join(null, System.Text.RegularExpressions.Regex.Split(str, "[^\\d]"));
    LVL 26

    Accepted Solution

    how about

    public string GetNumberFromStr(string str)
                string ss = "";
                foreach (char s in str)
                    int a = 0;
                    if (int.TryParse(Convert.ToString(s), out a))
                        ss += a;

                return ss;

    Author Comment


    Can we remove the chat "^" in the regular expression?

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    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…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    728 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

    21 Experts available now in Live!

    Get 1:1 Help Now