How to write Regex for numbers only in C#?

Regular expressions (regex) are patterns used to match specific text formats. In C#, you can create regex patterns to validate that input contains only numbers. The System.Text.RegularExpressions.Regex class provides methods to test strings against these patterns.

For numbers-only validation, you need to understand the key regex metacharacters and how to construct patterns that accept different numeric formats like integers, decimals, and negative numbers.

Syntax

Basic regex pattern for numbers only −

Regex regex = new Regex(@"^[0-9]+$");

Pattern for numbers with optional decimal point −

Regex regex = new Regex(@"^[0-9]+\.?[0-9]*$");

Pattern for numbers with optional negative sign −

Regex regex = new Regex(@"^-?[0-9]+$");

Key Regex Metacharacters for Numbers

Metacharacter Description Example
^ Start of string ^123 matches "123abc" but not "a123"
$ End of string 123$ matches "a123" but not "123abc"
[0-9] Any digit from 0 to 9 [0-9] matches any single digit
+ One or more occurrences [0-9]+ matches "123" but not ""
* Zero or more occurrences [0-9]* matches "123" and ""
? Zero or one occurrence -? allows optional negative sign

Using Regex for Integers Only

This pattern validates strings containing only positive integers −

using System;
using System.Text.RegularExpressions;

class Program {
    public static void Main() {
        string[] testNumbers = {"123", "123abc", "abc123", "0", "007"};
        Regex regex = new Regex(@"^[0-9]+$");
        
        foreach(string num in testNumbers) {
            bool isValid = regex.IsMatch(num);
            Console.WriteLine($"'{num}' is valid integer: {isValid}");
        }
    }
}

The output of the above code is −

'123' is valid integer: True
'123abc' is valid integer: False
'abc123' is valid integer: False
'0' is valid integer: True
'007' is valid integer: True

Using Regex for Decimal Numbers

This pattern allows decimal numbers with optional fractional parts −

using System;
using System.Text.RegularExpressions;

class Program {
    public static void Main() {
        string[] testNumbers = {"123", "123.45", "123.", ".45", "123.45.67"};
        Regex regex = new Regex(@"^[0-9]+\.?[0-9]*$");
        
        foreach(string num in testNumbers) {
            bool isValid = regex.IsMatch(num);
            Console.WriteLine($"'{num}' is valid decimal: {isValid}");
        }
    }
}

The output of the above code is −

'123' is valid decimal: True
'123.45' is valid decimal: True
'123.' is valid decimal: True
'.45' is valid decimal: False
'123.45.67' is valid decimal: False

Using Regex for Signed Numbers

This pattern allows positive and negative numbers including decimals −

using System;
using System.Text.RegularExpressions;

class Program {
    public static void Main() {
        string[] testNumbers = {"123", "-123", "123.45", "-123.45", "--123", "12-3"};
        Regex regex = new Regex(@"^-?[0-9]+\.?[0-9]*$");
        
        foreach(string num in testNumbers) {
            bool isValid = regex.IsMatch(num);
            Console.WriteLine($"'{num}' is valid signed number: {isValid}");
        }
    }
}

The output of the above code is −

'123' is valid signed number: True
'-123' is valid signed number: True
'123.45' is valid signed number: True
'-123.45' is valid signed number: True
'--123' is valid signed number: False
'12-3' is valid signed number: False

Common Regex Patterns for Numbers

Pattern Description Example Matches
^[0-9]+$ Positive integers only 123, 0, 9999
^-?[0-9]+$ Signed integers 123, -123, 0
^[0-9]*\.?[0-9]+$ Decimal numbers 123.45, .45, 123
^\d+$ Alternative digit pattern 123, 456 (same as [0-9]+)

Conclusion

Regular expressions provide powerful pattern matching for validating numbers-only input in C#. Use ^[0-9]+$ for integers, ^[0-9]*\.?[0-9]+$ for decimals, and add -? prefix to allow negative numbers. The Regex.IsMatch() method efficiently tests whether strings conform to your numeric patterns.

Updated on: 2026-03-17T07:04:36+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements