C#基于模式生成随机字符串 [英] c# generate random string based on pattern

查看:127
本文介绍了C#基于模式生成随机字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于模式做一个简单的字符串生成。

我的想法是使用Regex做简单的替换。
我开始使用简单的方法:

I'm trying to do a simple string generation based on pattern.
My idea was to use Regex to do simple replace. I've started with simple method:

private static string parseTemplate(string template)
{
    return Regex.Replace(template, @"(\[d)((:)?([\d]+)?)\]", RandomDigit());
}

private static string RandomDigit()
{
    Random r = new Random();
    return r.Next(0, 9).ToString();
}

现在这是替换 d> [d:3] 与应该是随机数字。

不幸的是,例如如果我把 test [d] [d] [d:3] 我的方法将返回 test 222

我想在每个地方获得不同的数字,例如 test 361

What this does for now is replacing groups like [d], [d:3] with what supposed to be random digit.
Unfortunately every group is replaced with the same digit, for example if I put test [d][d][d:3] my method will return test 222.
I would like to get different digit in every place, like test 361.

第二个问题我有办法处理长度:

Second problem I have is way to handle length:

现在我必须指定 [d] 我想要的每个数字,但更容易指定 [d:3] 并获得相同的输出。

right now I must specify [d] for every digit I want, but it would be easier to specify [d:3] and get the same output.

我知道有一个名为费用的项目,但我想在没有此项目的情况下这样做库

I know that there is a project called Fare, but I would like to do this without this library

现在我只搜索 [d] 将不会有问题添加其他组,例如: [s] 用于特殊字符或任何其他类型的模式。

For now I only search for [d], but is this method will work fine there won't be a problem to add other groups for example: [s] for special characters or any other type of patters.

Edit1

根据建议,我将Random改为了一个静态变量,如下:

As it was suggested I changed Random to a static variable like so:

private static string parseTemplate(string template)
    {
        return Regex.Replace(template, @"(\[d)((:)?([\d]+)?)\]", RandomDigit());
    }

    private static Random r = new Random();

    private static string RandomDigit()
    {
        return r.Next(0, 9).ToString();
    }
Problem is that when I call my code like so:
Console.WriteLine(parseTemplate("test [d:2][d:][d]"));
Console.WriteLine(parseTemplate("test [d:2][d:][d]")); 

我得到这样的输出

test 222
test 555

(例如):

test 265
test 962

我认为这个问题是 Regex.Replace ,它调用我的 RandomDigit 只有一次。

I think that problem is with Regex.Replace which calls my RandomDigit only once.

推荐答案

Assumnig [d:3] 表示三个随机数字,以下 MatchEvaluator 使用长度(从组4读取)生成随机数字字符串:

Assumnig [d:3] means "three random digits", the following MatchEvaluator uses the length (read from group 4) to generate a random digit string:

static string ReplaceSingleMatch(Match m)
{
    int length;
    if (!int.TryParse(m.Groups[1].Value, out length))
        length = 1;
    char[] chars = new char[length];
    for (int i = 0; i < chars.Length; i++)
        chars[i] = RandomDigit()[0];
    return new string(chars);
}

然后可以按如下方式调用:

You can then call this as follows:

private static string parseTemplate(string template)
{
    return Regex.Replace(template, @"\[d(?::(\d+))?\]", ReplaceSingleMatch);
}

您可能需要更改 RandomDigit 返回单个 char 而不是字符串,或者返回 int 并返回多个字符。

You might want to then change RandomDigit to return a single char rather than a string, or to take an int and return multiple characters.

这篇关于C#基于模式生成随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆