Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4055366
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:41:33+00:00 2026-05-20T14:41:33+00:00

Hi I need regex in c sharp to display SSN in the format of

  • 0

Hi I need regex in c sharp to display SSN in the format of xxx-xx-6789. i.e 123456789 should be displayed as xxx-xx-6789 in a textfield. The code I am using write now is

string SSN = "123456789";
Regex ssnRegex = new Regex("(?:[0-9]{3})(?:[0-9]{2})(?:[0-9]{4})");
string formattedSSN = ssnRegex.Replace(SSN, "XXX-XX-${last}");

What is correct Reg Expression to mask ssn xxx-xx-6789 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:41:33+00:00Added an answer on May 20, 2026 at 2:41 pm

    You’re using last as a named group in your replacement string without specifying it in your pattern.

    Update your pattern as follows and your existing code should work:

    (?:[0-9]{3})(?:[0-9]{2})(?<last>[0-9]{4})
    

    Depending on your input you may want to restrict the pattern to match the entire input by using the ^ and $ metacharacters to match the start and end of the string, respectively. By doing so the regex won’t match an input with more than 9 consecutive numbers. This would look like:

    ^(?:[0-9]{3})(?:[0-9]{2})(?<last>[0-9]{4})$
    

    Also, since all you care about is the last 4 digits, you might choose to match 5 numbers, followed by 4 numbers, instead of splitting it up into 3 groups:

    ^(?:[0-9]{5})(?<last>[0-9]{4})$
    

    In addition, if your input is always 9 numbers and can be trusted, then regex is not needed. You could simply get the substring to extract the last 4 characters:

    string SSN = "123456789";
    string formattedSSN = "XXX-XX-" + SSN.Substring(SSN.Length - 4, 4);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For jquery validation i need regex for SSN number format and EIN number format
Possible Duplicate: What is the ultimate postal code and zip regex? I need Regex
I have unfortunately wandered into a situation where I need regex using Ruby. Basically
I use webBrowser.DocumentText to get the html code of a page. using Regex, i
I need regex for Regular Expression Validator in ASP.NET, that user need to write
I need a regex that also matches Chinese, Greek, Russian, ... letters. What I
I need a Regex I think to see if a string is only a
I need a regex for four-digit numbers separated by comma (default can also be
I need a regex that matches the following (the brackets indicate I want to
I need a regex that will give me the following results from each example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.