How to Find SSN Using a Regular Expression in C#
The article explains how to find social security number aka SSN from a text document. The article uses C# regular expressions to do so. In this article, you will see how to read an SSN number containing generic numbers as well as SNS numbers in a specific format.
Finding SSN Containing Numbers Only
Let’s first see how to read SSN numbers of any length and without any dashes or any special character.
Look at the following text receipt. It contains some information about the SSN number of a customer who purchased some food items.
To extract the SSN number from the above script, you can use the following script.
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = File.ReadAllText(@"E:\Datasets\invoice.txt", Encoding.UTF8); Console.WriteLine("===================="); var myRegex = new Regex(@"(SSN #: \s*\d*)", RegexOptions.IgnoreCase); string result = myRegex.Match(textFile).ToString(); Console.WriteLine(result); } } }
The script above reads the text file containing the SSN number and then uses the regular expression “SSN #: \s\d”. The regular expression consists of 3 parts. It first finds the text “SSN #” and then allows any number of spaces using the expression “\s”. Finally, the digits in the SSN number are read via the “\d” part of the expression. Here is the output of the script above.
Finding SSN in a Specific Format
Though SSN number is often used without dashes, most of the time SSN number is in the following format XXX-XX-XXXX i.e. 3 digits followed by a dash, then 2 digits, followed by a dash, and finally 4 digits at the end. An example of such an SSN number is shown in the following receipt.
To read the SSN number such as the one in the above format, the following script can be used.
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = File.ReadAllText(@"E:\Datasets\invoice.txt", Encoding.UTF8); Console.WriteLine("===================="); var myRegex = new Regex(@"SSN #: \d{3}-\d{2}-\d{4}", RegexOptions.IgnoreCase); string result = myRegex.Match(textFile).ToString(); Console.WriteLine(result); } } }
The above code first reads the text document that contains the SSN number The script uses “SSN #: \d{3}-\d{2}-\d{4}” regex expression which reads digits in the format XXX-XX-XXXX that follow the text “SSN #:”. The output of the above script is as follows:
You can see from the output that SSN has been successfully read.
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