Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
Is there a regex to mask middle areas of a dynamic alphanumeric string?

I am trying to mask some confidential information which can be any alphanumeric string for ex .

Example 1

Before masking - PMGKJGFWB125 After masking - PMG******125

Example 2

Before masking - 19000 After masking - 1**00

I was trying something like this in C#

Regex.Replace(s, @"\d(?!\d{0,3}$)", "*")

The length of string may vary so we cannot add hardcoded offsets .

Can I get some help on this ?

Thanks in advance

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Looks like, roughly roughly, you want to divide your string up into quarters and then star out the middle two quarters

Don't get too technical..

var str = "PMGKJGFWB125".ToCharArray();

for(int x = str.Length/4; x < str.Length*3/4; x++)
  str[x] = '*';

return new string(str);

You can adjust the 4 for different length strings e.g. if your string is 6 or less maybe do 3 instead of 4.. etc

If you want it as LINQ, for a "WTH is that?!" laugh:

(int f, int t) = (str.Length/4, str.Length*3/4);
return string.Concat(str.Select((c,i)=>f<i&&i<t?'*':c)));
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!