随机化正则表达式字符串替换

时间:2011-11-22 20:31:27

标签: c# regex string random

我正在尝试做两件事,首先,为字符串提供伪本地化(leet speak)转换,其次,根据百分比比例随机化替换字符串中包含的字符,IE:every 5%-30%的时间......

另外,除此之外,我需要在字符串的开头,中间或结尾(随机)中添加固定数字或特殊字符。

每个人都知道,这是一个随机密码生成器。

我的问题是,如何将所述功能合并到我目前正在使用的代码库中?更好的是,我怎样才能有效地做到这一点?

感谢您提供的任何帮助。

namespace pseudoLocalizer
{
class RegexTuple
{
    Regex thisRegex;
    string replaceWith;

    public RegexTuple(string expression, string replacement)
    {
        thisRegex = new Regex(expression);
        replaceWith = replacement;

    }

    public string GetReplaceWith()
    {
        return replaceWith;
    }

    public Regex GetRegex()
    {
        return thisRegex;
    }
}

class pLocalizeEngine
{
    //leet table.
    RegexTuple[] arrRegexTuples = {            
    new RegexTuple ("a", @"@"),
    new RegexTuple ("A", @"4"),
    new RegexTuple ("b", @"6"),
    new RegexTuple ("B", @"8"),
    new RegexTuple ("c", @"<"),
    new RegexTuple ("C", @"("),
    new RegexTuple ("d", @"d"),
    new RegexTuple ("D", @"D"),
    new RegexTuple ("e", @"3"),
    new RegexTuple ("E", @"3"),
    new RegexTuple ("f", @"f"),
    new RegexTuple ("F", @"F"),
    new RegexTuple ("g", @"6"),
    new RegexTuple ("G", @"9"),
    new RegexTuple ("h", @"#"),
    new RegexTuple ("H", @"#"),
    new RegexTuple ("i", @"!"),
    new RegexTuple ("I", @"1"),
    new RegexTuple ("j", @"j"),
    new RegexTuple ("J", @"J"),
    new RegexTuple ("k", @"k"),
    new RegexTuple ("K", @"K"),
    new RegexTuple ("l", @"1"),
    new RegexTuple ("L", @"7"),
    new RegexTuple ("m", @"m"),
    new RegexTuple ("M", @"M"),
    new RegexTuple ("n", @"~"),
    new RegexTuple ("N", @"N"),
    new RegexTuple ("o", @"0"),
    new RegexTuple ("O", @"0"),
    new RegexTuple ("p", @"p"),
    new RegexTuple ("P", @"P"),
    new RegexTuple ("q", @"q"),
    new RegexTuple ("Q", @"Q"),
    new RegexTuple ("r", @"2"),
    new RegexTuple ("R", @"2"),
    new RegexTuple ("s", @"$"),
    new RegexTuple ("S", @"5"),
    new RegexTuple ("t", @"+"),
    new RegexTuple ("T", @"7"),
    new RegexTuple ("u", @"u"),
    new RegexTuple ("U", @"U"),
    new RegexTuple ("v", @"v"),
    new RegexTuple ("V", @"V"),
    new RegexTuple ("w", @"w"),
    new RegexTuple ("W", @"W"),
    new RegexTuple ("x", @"x"),
    new RegexTuple ("X", @"X"),
    new RegexTuple ("y", @"y"),
    new RegexTuple ("Y", @"Y"),
    new RegexTuple ("z", @"2"),
    new RegexTuple ("Z", @"2"),
};

    public pLocalizeEngine()
    {
        //
    }

    public string Localize(string oldString)
    {
        string pLocalString = oldString;
        foreach (RegexTuple tuple in arrRegexTuples)
        {
            pLocalString = tuple.GetRegex().Replace(pLocalString, tuple.GetReplaceWith());
        }
        return pLocalString;
    }
}

1 个答案:

答案 0 :(得分:2)

假设您需要密码生成器,我建议您:

private const string ValidCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890@<()!?~+";
private Random random = new Random();

private string RandomPasswordGenerator(int length)
{
    string password = string.Empty;
    while (length > 0)
    {
        password += ValidCharacters[random.Next(0, ValidCharacters.Length - 1)];
        length--;
    }
    return password;
}