简体   繁体   中英

Randomizing regex string replacement

I'm trying to do two things here, first, provide pseudo localization (leet speak) conversion for a string, and secondly, randomize the chance to replace a character contained in the string based on a percentage ratio, IE: every 5%-30% of the time...

Also, in addition to this, I'm needing to add a fixed number or special chars in either the start of the string, middle or end (randomized).

Just so everyones aware, this is for a random password generator.

My question is, how would I incorporate said functionality into the code base I currently am working with? better yet, how can I do this efficiently?

Thank you for any help you're able to provide with this.

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;
    }
}

Assuming you need a password generator, here is my suggestion for you:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM