I have some IBAN code. For example it is Germany IBAN: DE12500123456789012345. I need to mask it and get result like this: DE*****12345*******345. But since the length of IBAN is different for each country I have following rule for masking: {2 letters}{5 asterisks}{5 numbers}{n asterisks}{3 numbere}. So in case DE IBAN - DE*****12345*******345, in case BE BE*****12345*345.
How to implement it with Regex for C#?
Well not using Regex will be more easier and more performant in your case :
Fiddle
Edit A regex solution which not as clean as the above, It uses named groups and a modified extension method that replaces named groups that is taken from this SO answer
Fiddle
Edit Added @Wiktor Stribiżew solution to the benchmark:
Performance comparison of the three methods:
BenchmarkFiddle