How to Find a Number Using Regular Expressions in C#
A text string is the most versatile data type in C#. A string can store alphabets, numbers, spaces, special characters, in short virtually everything in the form of a sequence of characters. Often times you need to find a specific data type within a string. For instance, you might want to look for special characters, numbers, or boolean values inside a string. Regular expressions can be used for such tasks.
In this article, you will see how to find numbers within a text string using regular expressions in C#. Therefore, let’s begin without ado.
Finding all Numbers in a String
To find all the numbers in a string, you can use the Split() function from Regex. The first parameter to the Split() function is the string that contains numbers that you want to find. The second parameter is the regex expression used to find numbers in a string. The regex expression “\D+” returns numbers in a string in the form of an array.
Look at the following example. In the following script, the input string contains 4 numbers. The Regex Split() function will return an array that contains the numbers in the input string. The numbers are then printed on the console via a for each loop.
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string input = "A year has 365 days, 52 weeks. There are 7 days in a week. A week has 24 hours"; string[] numbers = Regex.Split(input, @"\D+"); foreach (string num in numbers) Console.WriteLine(num); } } }
Here is the output of the above script:
365 52 7 24
Finding a Number Between Two Strings
Often times you need to find a number between two strings. Finding a number between two strings is actually similar to finding a string between two strings. Once a number is found, you can convert the number from string to integer.
To find a number between two strings, you can use the Match() function from Regex. The first parameter to the Match() function is the string that contains numbers that you want to find; the second parameter is the Regex pattern that finds a number between two string values.
The following script tries to find a number that exists between two strings “has” and “days”.
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string input = "A year has 365 days and 52 weeks."; string match = Regex.Match(input, @"has (.+?) days ").Groups[1].Value; match = match.Trim(); int num_match = int.Parse(match); Console.WriteLine(match); } } }
The Match() function returns a string. In the above script, to convert string to numbers we first remove all the empty spaces from the string using the Trim() function. Next, int.Parse() function is used to convert the string to an integer. Finally, the integer is printed on the screen.
In the above script, we have a number 365 between the strings “has” and “days”. When you run the above script, you should see the following output:
365
Other useful articles:
- How to Use RegEx for Data Extraction
- How to Find Total Tax Using a Regular Expression in C#
- How to Find a Number Using Regular Expressions in C#
- How to Find Invoice Numbers Using Regular Expressions in C#
- Find SSN Using a Regular Expression in C#
- Find Total Amount Using a Regular Expression in C#
- How to Find Website Links using Regex
- Module 1: Regular Expressions for Beginners
- Module 1: Regex Usage and Tool Demo
- Module 2: Regex Engine Basics (Part 1)
- Module 2: Regex Engine Basics (Part 2)
- Module 2: Regex Syntax in Detail (Part 1)
- Module 2: Regex Syntax in Detail (Part 2)
- Module 2: Quantifiers in Reg Ex for Beginners
- Module 2: Short Codes in Reg Ex for Beginners
- Module 2: Anchors and Boundaries in Detail
- Module 2: Grouping and Subpattern in Detail
- Module 3: Realtime Use Case of Regular Expressions - Part 1
- Module 3: Realtime Use Case of Regular Expressions - Part 2
- Module 3: Realtime Use Case of Regular Expressions - Part 3
- Module 3: Realtime Use Case of Regular Expressions - Part 4
- How to Find Quantity Field Using Regular Expression in C#
- How to Find Phone Numbers without a Specific Format
- How to Find Date Using Regular Expression in C#
- How to Find Time Using Regular Expression in C#
- How to Find a Sentence Using Regular Expressions in C#
- Find a Word Using Regular Expression in C#
- Find a Due Date using Regular Expressions in C#
- How to Find the End of a String Using Regular Expression in C
- How to Find the Start of a String Using Regular Expression in C
- How to Find a Comma using Regular Expression in C Sharp
- How to Find a Dot using Regular Expression in C
- How to Find a Semicolon using Regular Expression in C Sharp
- How to Find a Double Space using Regular Expression in C