Extracting Numbers from Strings Using Regular Expressions in C#

Nov 23, 2025 · Programming · 12 views · 7.8

Keywords: Regular Expressions | C# Programming | String Processing | Number Extraction | XML Parsing

Abstract: This article provides a comprehensive guide to extracting numerical values from strings containing non-digit characters using regular expressions in C#. It thoroughly explains the meaning and application scenarios of patterns like \d+ and -?\d+, demonstrates the usage of Regex.Match() and Regex.Replace() functions with complete code examples, and compares different methods based on their suitability. The discussion also covers escape character handling and performance optimization recommendations, offering practical guidance for real-world scenarios such as XML data parsing.

Fundamental Concepts of Regular Expressions

In string processing, regular expressions provide a powerful and flexible mechanism for pattern matching. Understanding the basic syntax of regular expressions is crucial for common tasks like number extraction.

Core Regular Expression Patterns

The \d+ pattern is the most commonly used solution for number extraction:

Complete Implementation Code

The following code demonstrates the standard approach using Regex.Match():

string input = stringThatHaveCharacters.Trim();
Match match = Regex.Match(input, @"\d+");
int number = Convert.ToInt32(match.Value);
return number;

Extended Pattern for Negative Numbers

When dealing with numbers that may include negative signs, the -?\d+ pattern can be used:

Alternative Method Comparison

Another common approach uses Regex.Replace() to remove all non-digit characters:

string input = "1-205-330-2342";
string result = Regex.Replace(input, @"[^\d]", "");
Console.WriteLine(result); // Output: 12053302342

Here, [^\d] matches any non-digit character, making this method suitable for extracting scattered digit sequences.

Practical Application Scenarios

When processing XML data, mixed-format numeric strings are frequently encountered. For example:

Performance and Best Practices

For high-frequency usage scenarios, precompiling the regular expression is recommended:

private static readonly Regex NumberRegex = new Regex(@"\d+", RegexOptions.Compiled);

Additionally, proper exception handling should be implemented since Convert.ToInt32() may throw exceptions when no digits are present in the string.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.