Regex - Need to replace a pattern found with a * mask

Hey,

It doesn't matter how I find the pattern I'm interested in, the thing is I want to replace all the chars that belong to the captured text into a mask.

Let's say I'm working on a big message : msg
I would like to catch: the following Regex reg = new Regex("CREDIT CARD:(?<PatternToMask>.*?)")
And replace each character in the result into a new char = *

Thanks,
breezbackAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Richard QuadlingSenior Software DeveloperCommented:
I think you are going to have to do this in 2 parts.

The first part is to extract the match, then get the length of the match and then replace it with a new repeated string of *.

You also could use something like this ...

(CREDIT CARD:\**)\d

and repeated replace this with ...

$1*

until there are no more matches.


0
iHadiCommented:
Hi breezback

Try the following code in a console application
        static void Main(string[] args)
        {
            string str = ProcessString("CREDIT CARD:ABCD;CREDIT CARD:EFGHhhggfgh");
 
            Console.WriteLine(str);
        }
 
        static string ProcessString(string str)
        {
            MatchCollection matches = Regex.Matches(str, @"CREDIT CARD:(?<PatternToMask>[\w]*)");
            if (matches.Count == 0)
                return "";
 
            for (int i = 0; i < matches.Count; i++)
            {
                int index = matches[i].Groups["PatternToMask"].Index;
                int length = matches[i].Groups["PatternToMask"].Value.Length;
                string tmpstr = "";
 
                tmpstr = tmpstr.PadLeft(length, '*');
 
                str = str.Remove(index,length);
                str = str.Insert(index, tmpstr);
            }
            return str;
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
breezbackAuthor Commented:
Excellent, do you think it is optimized (my application is speed-critical)?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.