Loading ...

Regex: Get number only from a string | CodeAsp.Net

Regex: Get number only from a string

0 (0votes)

In this blog I will show how to get number only from a string with REGEX. Let's say we have a string "Hello 123456789 user 456 welcome." and we want the output to be "123456789456". Below is the sample code to do the same:

using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace SampleConsoleApplication
    class Program
        static void Main(string[] args)
            const string inputString = @"Hello 123456789 user 456 welcome.";

            string output = Regex.Replace(inputString, @"[^\d]", ""); ;



Below is the explanation of regex:


Match any character that is NOT a “A single digit 0..9” «[^\d]»

Created with RegexBuddy

Comments (no comments yet)

Top Posts