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

  • Home
  • SEARCH
  • 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 7248123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:05:47+00:00 2026-05-28T22:05:47+00:00

I am trying to replace 1234567890123456 with ************3456 with the following code: Regex.Replace(xml,@\b\d{13,16}\b, string.Concat(new

  • 0

I am trying to replace 1234567890123456 with ************3456 with the following code:

Regex.Replace(xml,@"\b\d{13,16}\b", string.Concat(new String('*',12),a.Value.Substring(a.Value.Length - 4)));

Where a.Value is the number, but it just results in:

****************

Complete code:

//Check if value is the credit card
        if(a.Value.Length >= 13 && a.Value.Length <= 16)
                xml = Regex.Replace(xml,@"\b\d{12}(?=\d{4})\b", new String('*',12));
        else //If value is not a credit card, replace it with ***
            xml = Regex.Replace(xml,@"\b\d+\b", "***");

I replaced the first part of the if statement with:

Regex.Replace(xml, @"\b\d{13,16}\b", match => 
    String.Concat(new String('*', match.Length - 4), match.Value.Substring(match.Length - 4)));

but I still get *** for the cardnum.

Here is the XML. Note the card number can be between 13 and 16 digits and I always want to keep the last 4.

<Details>
<CreditCard cardnum='1234567890123456'
ccv='123' 
exp='0212' 
cardType='1' 
name='joe' />
</Details>

long numeric;

    string xml = @"<Details>
<CreditCard cardnum='1234567890123456'
ccv='123' 
exp='0212' 
cardType='1' 
name='joe' />
</Details>";

XElement element = XElement.Parse(xml);
IEnumerable<XElement> elementsWithPossibleCCNumbers = 
        element.Descendants()
               .Where(d => d.Attributes()
                            .Where(a => a.Value.Length >= 13 && a.Value.Length <= 16)
                            .Where(a => long.TryParse(a.Value, out numeric))
                            .Count() == 1).Select(x=>x);


foreach(var x in elementsWithPossibleCCNumbers)
{

   foreach(var a in x.Attributes())
   {

    //Check if the value is a number
    if(long.TryParse(a.Value,out numeric))
    {
        //Check if value is the credit card
        if(a.Value.Length >= 13 && a.Value.Length <= 16)
            Regex.Replace(xml,@"\b\d{12}(?=\d{4}\b)", new String('*',12));
        else //If value is not a credit card, replace it with ***
            xml = Regex.Replace(xml,@"\b\d+\b", "***");
    }
   }
}
  • 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-28T22:05:47+00:00Added an answer on May 28, 2026 at 10:05 pm

    You can use lookahead to match the trailing 4 digits but preserve them:

    Regex.Replace(xml,@"\b\d{12}(?=\d{4}\b)", new String('*',12))
    

    Edit: This version is closer to your code. However, rather than relying on a pre-set a.Value, it uses the match variable to allow you to apply your transformation on whatever was actually captured in your regex.

    Regex.Replace(xml, @"\b\d{13,16}\b", match => 
        new String('*', match.Length - 4) +
        match.Value.Substring(match.Length - 4));
    

    The above code assumes that you want your redacted string to have the same length as the original, and preserve the last 4 digits.

    If you want to redact the first 12 digits and preserve whatever remains (1–4 digits), use:

    Regex.Replace(xml, @"\b\d{13,16}\b", match => 
        new String('*', 12) +
        match.Value.Substring(12));
    

    Edit: I think you already got it. But probably what you need to do is replace:

    xml = Regex.Replace(xml, …
    

    with:

    a.Value = Regex.Replace(a.Value, …
    

    Edit: I went through your now-complete code, and I realized that you don’t even need regex for what you’re trying to accomplish, since you’re replacing the attribute value in entirety. Here’s how I would adapt your code (with a number of changes):

    string xml = @"
        <Details>
            <CreditCard cardnum='1234567890123456'
                        ccv='123' 
                        exp='0212' 
                        cardType='1' 
                        name='joe' />
        </Details>";
    
    XElement element = XElement.Parse(xml);
    IEnumerable<XElement> elementsWithPossibleCCNumbers =
        element.Descendants()
                .Where(d => d.Attributes()
                             .Select(a => a.Value)
                             .Any(v => v.Length >= 13 &&
                                       v.Length <= 16 &&
                                       v.All(Char.IsDigit)));
    
    foreach (var x in elementsWithPossibleCCNumbers)
    {
        foreach (var a in x.Attributes())
        {
            //Check if the value is a number
            if (a.Value.All(Char.IsDigit))
            {
                //Check if value is the credit card
                if (a.Value.Length >= 13 && a.Value.Length <= 16)
                    a.Value = new String('*', a.Value.Length - 4) + a.Value.Substring(a.Value.Length - 4);
                else //If value is not a credit card, replace it with ***
                    a.Value = "***";
            }
        }
    }
    
    xml = element.ToString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to replace some words from a string and use following code
I trying to replace string Resources.AppResource.Info; like this Switch(Info); Is this possible with regex?
Im trying to replace the following string in php: 2011.11.12 20:06,Teal'c Ostus,Solid Pyroxeres 46521,Pyroxeres
im trying to replace a remove unnessary new lines with preg-replace but my regex
I've been trying to replace to following string using the following... var r =
When trying to replace the content of an XML file in C#.NET with a
I'm trying to replace each , in the current file by a new line:
I am trying to replace the backslash (escape) character in a Javascript string literal.
I'm trying replace the index of form elements. I have the following var test
i am trying to replace spaces with underscore '_' in the following variable $e_type

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.