Link Search Menu Expand Document

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.

Find SSN

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.

Find SSN Invoices

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.

Find SSN with RegEx

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:

Find SSN with Regular Expressions

You can see from the output that SSN has been successfully read.  

Other useful articles:


Back to top

© , Regexsonline.com — All Rights Reserved - Terms of Use - Privacy Policy