return only Digits 0-9 from a String

Brian Boatright picture Brian Boatright · May 10, 2009 · Viewed 70.5k times · Source

I need a regular expression that I can use in VBScript and .NET that will return only the numbers that are found in a string.

For Example any of the following "strings" should return only 1231231234

  • 123 123 1234
  • (123) 123-1234
  • 123-123-1234
  • (123)123-1234
  • 123.123.1234
  • 123 123 1234
  • 1 2 3 1 2 3 1 2 3 4

This will be used in an email parser to find telephone numbers that customers may provide in the email and do a database search.

I may have missed a similar regex but I did search on regexlib.com.

[EDIT] - Added code generated by RegexBuddy after setting up musicfreak's answer

VBScript Code

Dim myRegExp, ResultString
Set myRegExp = New RegExp
myRegExp.Global = True
myRegExp.Pattern = "[^\d]"
ResultString = myRegExp.Replace(SubjectString, "")

VB.NET

Dim ResultString As String
Try
      Dim RegexObj As New Regex("[^\d]")
      ResultString = RegexObj.Replace(SubjectString, "")
Catch ex As ArgumentException
      'Syntax error in the regular expression
End Try

C#

string resultString = null;
try {
    Regex regexObj = new Regex(@"[^\d]");
    resultString = regexObj.Replace(subjectString, "");
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}

Answer

Matt Hamilton picture Matt Hamilton · May 10, 2009

In .NET, you could extract just the digits from the string. Like this:

string justNumbers = new String(text.Where(Char.IsDigit).ToArray());