Using VB or C#, I am getting a string of variable length from the database. This information is sensitive information that only certain users will be able to see.
I have two cases that will use the same logic (I think).
scenario 1: replace all characters with x
scenario 2: replace all characters with x except the last 4 characters (assume length > 4 – this check is being done).
I thought that this would be easiest using Regex.Replace(input, pattern, replacestring). As opposed to a lot of string handling with substrings and forcing a length of ‘x’s.
But it seems that Regex will always be my kryptonite.
Any help from the regex gurus would be appreciated. Alternatively a better solution would be welcome.
I’m not convinced that regular expressions are the best approach here, but these should work.
ReplaceWithXreplaces every single character (specified by.) with anx.ReplaceWithXLeave4replaces all but the last four characters with anx. It does this by matching any single character (.) while using a zero-width negative lookahead assertion to throw out this match for the last four characters.And for completeness, below is what it looks like when not using regular expressions. This approach is probably quite a bit faster than the regex approach, even though you might not ever see the perf difference when just doing it once or twice like these examples are. In other words, if you’re doing this on a server with lots of requests, avoid regex since it’s only marginally easier to read.