C# 检查字符串是否在C中具有指定的结构

C# 检查字符串是否在C中具有指定的结构,c#,string,C#,String,我有一个字符串数组,我想检查我的任何字符串是否具有指定的结构。例如,我的一个结构如下: The value '{0}' is not valid for {1}. 我如何在C语言中做到这一点?如果你真的想用正则表达式来做一些我认为像玩火一样的事情,你可以 public static Regex MakeRegex(string str) { // We temporarily replace the {x} with \0 str = string.Format(str, "\

我有一个字符串数组,我想检查我的任何字符串是否具有指定的结构。例如,我的一个结构如下:

The value '{0}' is not valid for {1}.

我如何在C语言中做到这一点?

如果你真的想用正则表达式来做一些我认为像玩火一样的事情,你可以

public static Regex MakeRegex(string str)
{
    // We temporarily replace the {x} with \0
    str = string.Format(str, "\0", "\0", "\0", "\0", "\0", "\0", "\0", "\0", "\0", "\0");

    // Then we escape the regex, so that for example the . is changed
    // to \.
    str = Regex.Escape(str);

    // We replace the \0 we had added in the beginning with .*
    str = str.Replace("\0", ".*");

    // We add the ^ and $ to anchor the regex
    str = "^" + str + "$";

    // The regex is complete
    return new Regex(str);
}
然后

请注意,我认为这不是一个好主意。。。但是有些人喜欢玩火

如果您想多次重用使用MakeRegex构建的rx,则应将其缓存


从我所做的一些小型基准测试中,我注意到。*?可能比快一点。*您更改str.Replace…

请注意,如果{0}==H'e'l'l'o…,这将很快中断。如何?请帮我做这件事:试着做一些基础研究,regex上有大量的信息,只要搜索一下就可以了。。。如果没有所有必要的信息,就不可能做出完整的回答。因此,这里不是为你做工作,而是帮助解决存在的问题。请说明您已经尝试和研究了哪些方法来解决问题,哪些方法没有奏效,以及为什么没有奏效
Regex rx = MakeRegex("The value '{0}' is not valid for {1}.");
bool ismatch = rx.IsMatch("The value '1' is not valid for Foo.");