【问题标题】:Regex Replace All Characters in Variable Length String正则表达式替换可变长度字符串中的所有字符
【发布时间】:2010-10-06 07:51:10
【问题描述】:

使用 VB 或 C#,我从数据库中得到一个可变长度的字符串。此信息属于敏感信息,只有特定用户才能看到。

我有两个案例会使用相同的逻辑(我认为)。

场景1:用x替换所有字符

场景 2:用 x 替换除最后 4 个字符之外的所有字符(假设长度 > 4 - 正在执行此检查)。

我认为使用 Regex.Replace(input, pattern, replacestring) 会最简单。与使用子字符串和强制长度为 'x' 的大量字符串处理相反。

但似乎 Regex 将永远是我的氪石。

正则表达式专家的任何帮助将不胜感激。或者,欢迎提供更好的解决方案。

【问题讨论】:

    标签: c# regex vb.net string


    【解决方案1】:

    我不相信正则表达式是这里最好的方法,但这些应该可以工作。

    ReplaceWithX 将每个字符(由. 指定)替换为x

    ReplaceWithXLeave4x 替换除了最后四个字符之外的所有字符。它通过匹配任何单个字符 (.) 来实现这一点,同时使用 zero-width negative lookahead assertion 来排除最后四个字符的匹配。

    using System;
    using System.Text.RegularExpressions;
    
    namespace ReplaceRegex
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(ReplaceWithX("12345678"));
                Console.WriteLine(ReplaceWithXLeave4("12345678"));
            }
    
            static string ReplaceWithX(string input)
            {
                return Regex.Replace(input, ".", "x");
            }
    
            static string ReplaceWithXLeave4(string input)
            {
                return Regex.Replace(input, ".(?!.{0,3}$)", "x");
            }
        }
    }
    

    为了完整起见,下面是不使用正则表达式时的样子。这种方法可能比正则表达式方法快很多,即使像这些示例一样只执行一次或两次时您可能永远看不到性能差异。换句话说,如果您在有大量请求的服务器上执行此操作,请避免使用正则表达式,因为它只是稍微容易阅读。

    using System;
    using System.Text;
    
    namespace ReplaceNoRegex
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(ReplaceWithX("12345678"));
                Console.WriteLine(ReplaceWithXLeave4("12345678"));
            }
    
            static string ReplaceWithX(string input)
            {
                return Repeat('x', input.Length);
            }
    
            static string ReplaceWithXLeave4(string input)
            {
                if (input.Length <= 4)
                    return input;
    
                return Repeat('x', input.Length - 4)
                     + input.Substring(input.Length - 4);
            }
    
            static string Repeat(char c, int count)
            {
                StringBuilder repeat = new StringBuilder(count);
    
                for (int i = 0; i < count; ++i)
                    repeat.Append(c);
    
                return repeat.ToString();
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      值得在这里指出(特别是考虑到“隐藏除最后四个字符之外的所有字符”这一点)敏感信息也可能因其长度而泄露。

      例如,如果我相当了解某人,找出他们的密码长度可能足以让我能够很好地猜测他们使用的密码;这当然足以帮助缩小可能性。

      因此我建议不要简单地用相同数量的'x'替换字符,而应该用固定数量的'x'替换,这样长度就无法猜测了。

      这就变成了一个非常简单的字符串替换案例。根本不需要正则表达式。

      在替换整个字符串的情况下,只需dislpay“xxxxxxxx”,不管原始字符串是什么(或者你喜欢的任何长度的'x')。

      在显示最后四个字符的情况下,只需使用substring()输出一个较短的'x'字符串,后跟最后四个字符。

      【讨论】:

      • 好点spudley。但这不适用于密码长度。更多的是联系方式,允许某些部门查看,而其他部门则不允许。在这种情况下,允许的最后四位数字是每个人都有权看到的邮政编码。业务规则。
      • 为什么不让他们只看到最后四个字符?
      • 就像我说的,业务规则。我不做 em,我只是编码 em :)
      猜你喜欢
      • 2016-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 2013-01-21
      相关资源
      最近更新 更多