Avatar of zhshqzyc
zhshqzyc

asked on 

Extract digit number from a string

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.
Regular ExpressionsC#

Avatar of undefined
Last Comment
zhshqzyc
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

http://www.java2s.com/Code/CSharp/Language-Basics/Useregularexpressiontogetnumberfromstring.htm
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

Avatar of zhshqzyc
zhshqzyc

ASKER

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?
     
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Try the following:
string str = "abc111west";
str = string.Join(null, System.Text.RegularExpressions.Regex.Split(str, "[^\\d]"));
ASKER CERTIFIED SOLUTION
Avatar of EDDYKT
EDDYKT
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of zhshqzyc
zhshqzyc

ASKER

@Dhaest:

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

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo