2
var panmaskednumber = "543034******0243"; Console.WriteLine(panmaskednumber.Count(x => x == '*'));
var pattern = "\\*";
var replace = "123456789";
Regex reg = new Regex(pattern);
var newnumber = reg.Replace(panmaskednumber, replace,panmaskednumber.Count(x => x == '*'));
Console.WriteLine(newnumber);

I'm trying to Replace * in var panmaskednumber(coming from DB with symmetric key).

Not liking to use the Contains approach in which I'm specifying number of * 6 / 7 with multiple If-elseif. Since those can vary between 6,7-9.

With my above approach it replaces for each char of -> * with var replace. Any Linq approach if there is highly appreciated. Result something: 5430341234567890243

7
  • 1
    What is the expected result? Commented Apr 12, 2017 at 12:52
  • 5430341234567890243 4 Commented Apr 12, 2017 at 12:53
  • Can you change your code to be mcve? Commented Apr 12, 2017 at 12:53
  • 1
    What sould be the output if *'s count is greater that replace's length? Commented Apr 12, 2017 at 13:03
  • 1
    You should be more specific about the input and desired output. "Result something" did not make clear the dependency between the number of * and the digits you expect as a replacement. Commented Apr 12, 2017 at 13:08

2 Answers 2

3

You need to use a \*+ pattern that will match 1 or more asterisk symbols:

var panmaskednumber = "543034******0243";
var replace = "123456789";
var res = Regex.Replace(panmaskednumber, @"\*+", replace);
// res => 5430341234567890243

See the C# demo.

If the number of asterisks to be replaced depends on the replace length, you may pass the match value to the match evaluator and perform necessary manipulatons there:

var panmaskednumber = "543034*****0243";
var replace = "123";

var res = Regex.Replace(panmaskednumber, @"\*+", m => 
      m.Value.Length <= replace.Length ? 
        replace.Substring(0, m.Value.Length) : 
        $"{replace}{m.Value.Substring(replace.Length)}"
    );
Console.Write(res);
// "543034***0243" / "123456789" -> 543034 123 0243
// "543034*****0243" / "123" -> 543034 123** 0243

See antother C# demo

Sign up to request clarification or add additional context in comments.

8 Comments

The expected out would be 5430341234560243 - the OP wants to replace the exact number of asterisk symbols with the matching number of digits.
Well, if the number of asterisks to be matches depends on the replace length, it still can be managed. Let me update.
@Rawling OP has some difficulties explaing but states "Since those can vary between 6,7-9." and "With my above approach it replaces for each char of -> * with var replace."
|
2

You can use a Regex, but you can achieve without. Lets go for a simpler solution. Try it online.

var panmaskednumber = "543034******0243";
var count = panmaskednumber.Count(x => x == '*');
var start = panmaskednumber.IndexOf('*');   
var replace = "123456789";

// output 5430341234567890243 (543034 123456789 0243)
Console.WriteLine(panmaskednumber.Remove(start) // get head
                + replace // add replace
                + panmaskednumber.Substring(start + count)); // add tail

 // output 5430341234560243 (543034 123456 0243) // get head 
 Console.WriteLine(panmaskednumber.Remove(start)
                + replace.Remove(count) // add replace with count respect
                + panmaskednumber.Substring(start + count)); // add tail

 replace = "123";

 // output 543034123***0243 (543034 123*** 0243) // get head 
 Console.WriteLine(panmaskednumber.Remove(start)
                + replace // add replace
                + new string('*', count - replace.Length) // fill with missing *
                + panmaskednumber.Substring(start + count)); // add tail

"I suppose it is tempting, if the only tool you have is a hammer, to treat everything as if it were a nail."

Law of instrument

Dont use Regex, if you don't have too. For this problem, C#.NET is enough. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.